In Delphi 2009 and later versions, string type implicitly equal to UnicodeString type. My discipline now is to use explicit UnicodeString type for my recent base units to eliminate the confusion. Is there a compiler directive that will make string <> UnicodeString in the unit where it was stated?
In Delphi 2009 and later versions, string type implicitly equal to UnicodeString type. My
Share
If you want to have a codebase that can be compiled under Unicode and non-Unicode Delphi’s you should be aware of the usage of each occurance of
string– is it a string being passed to a Windows API? Do you want to call the ‘Delphi native’ version, or do you want to call the Ansi version or Wide version explicitly? Is it a string being exchanged with RTL/VCL code? Is it a string from a database? Does it need to support Unicode, Ansi or any other encoding? Etc.In my experience, code interacting with Delphi RTL/VCL and WinAPI’s (as declared in Windows.pas and such) are best being served with
stringitself, as it transparently means AnsiString or UnicodeString, depending on the compiler being used. If the specific purpose of the string makes the distinction Ansi or Unicode important, use AnsiSting or UnicodeString explicitly. This introduces a problem with older Delphi’s as they don’t have a UnicodeString. In practice this can largly be solved by defining a UncodeString yourself in some central unit like this:If on the other hand you want your code to be configurable to use Ansi or Unicode, use your own string type as often as possible. Define it something like this :
.. and work with that in your own code.