Delphi has a reserved word resourcestring for ensuring that strings allow localization.
resourcestring keyword works like this:
resourcestring
msg='Hello, World';
begin
ShowMessage (msg);
end.
Delphi automatically replaces the reference to msg with a call to the system function LoadResString(), which retrieves the string "Hello, World" from the resource part of the executable. The Delphi ITE achieves its translation mechanism by redirecting these fetches to other files.
By including gnugettext.pas in your project, these fetches are replaced with another function, which translates all the strings. The default is, that resourcestrings are translated using the default textdomain, i.e. default.mo. In case you want the system to search other mo files, if the translation isn't found in default.mo just make some calls to the AddDomainForResourceString.
From this table:
http://www.netcoole.com/delphi2cs/statements.htm
we could see that C# corresponding statement is const string res = “bla-bla string”
At our project we have custom stringresourcetool which is able to generate classes with const strings properties basing on text files at design time in VS.
So next line in *.txt file:
msg=Hello, World
will generate:
public static string msg{get…}
which gets actual string from generated resource file.
When we are talking about resource files for globalization C# has easy way to fetch required strings. We could have files
like YourApplicationResources.uk-UA.resx for Ukrainian culture and so on... So application will be grabbing needed strings from needed files.
Interesting fact on theme:
“NASA’s Mars Climate Orbiter was lost on September 23, 1999 at a cost of $125 million because one engineering team used metric units, while another one used inches for a key spacecraft operation. When writing applications for international distribution, different cultures and regions must be kept in mind.”
Wednesday, November 25, 2009
Monday, November 16, 2009
Implement IDisposable Correctly
Running FxCop on some old stuff code gives me next error: “Implement IDisposable Correctly”.
And provided me with link to MSDN: http://msdn2.microsoft.com/ms244737(VS.90).aspx
I took look at existing code and it looked like:
At first I added GC.SuppressFinalize(this); like here:
You also need to have re-overridden Finalize.
So code which passed FxCop verification looks like:
Please go to MSDN page and read it, since to know how to implement IDisposable correctly is important thing.
And provided me with link to MSDN: http://msdn2.microsoft.com/ms244737(VS.90).aspx
I took look at existing code and it looked like:
#region IDisposable Members
void IDisposable.Dispose()
{
ShutdownServices();
}
#endregion
And here are lot of incorrect implementation of the IDisposable.void IDisposable.Dispose()
{
ShutdownServices();
}
#endregion
At first I added GC.SuppressFinalize(this); like here:
void IDisposable.Dispose()
{
ShutdownServices();
GC.SuppressFinalize(this);
}
Of course it was not enough – it is needed to call Dispose(true) because my class is not sealed.{
ShutdownServices();
GC.SuppressFinalize(this);
}
You also need to have re-overridden Finalize.
So code which passed FxCop verification looks like:
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~ServiceLoader()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
ShutdownServices();
}
}
#endregion
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~ServiceLoader()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
ShutdownServices();
}
}
#endregion
Please go to MSDN page and read it, since to know how to implement IDisposable correctly is important thing.
Sunday, November 15, 2009
Use Arg ONLY within a mock method call while recording
If you see this error when testing with RhinoMocks
Actually this should be all you need.
I faced this when doing test like this:
I want to mention that when testing with RhinoMocks you always need to pay attention on things like virtual methods, using interfaces instead of classes, also using protected instead of private to have possibility test those stuff with RhinoMocks.
System.InvalidOperationException: Use Arg<T> ONLY within a mock method call while recording. 0 arguments expected, 2 have been defined.
ensure that method on which you setuped expectation is virtual.Actually this should be all you need.
I faced this when doing test like this:
[Test]
public void Start_Service_InitializeWasCalled()
{
var service = MockRepository.GenerateMock<SpecificServiceBase>();
//some setup here...
service.Expect(x => x.Initialize(Arg<ServiceSettingElement>.Is.Anything, Arg<LogDelegate>.Is.Anything)).Repeat.Once();
serviceControllerBase.Start();
service.VerifyAllExpectations();
}
As you see I'm trying to verify that method Initialize was called after Start was triggered. Signature of method looks like:public void Start_Service_InitializeWasCalled()
{
var service = MockRepository.GenerateMock<SpecificServiceBase>();
//some setup here...
service.Expect(x => x.Initialize(Arg<ServiceSettingElement>.Is.Anything, Arg<LogDelegate>.Is.Anything)).Repeat.Once();
serviceControllerBase.Start();
service.VerifyAllExpectations();
}
public void Initialize(ServiceSettingElement configuration, LogDelegate logDelegate){...}
After I changed it to:public virtual void Initialize(ServiceSettingElement configuration, LogDelegate logDelegate){...}
My test passed ok.I want to mention that when testing with RhinoMocks you always need to pay attention on things like virtual methods, using interfaces instead of classes, also using protected instead of private to have possibility test those stuff with RhinoMocks.
Subscribe to:
Posts (Atom)





