tags:

views:

126

answers:

3

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?

+5  A: 

Unfortunately, the answer is No. There is no compiler switch for this.

GrandmasterB
This question has been asked repeatedly, and discussed in great detail, in the Embarcadero forums since D2009's release.
Remy Lebeau - TeamB
Thanks GranmasterB and Remy Lebeau for the information.
Phantom
A: 

So, you don't want to use String at all? Or revert it back to AnsiString?

Well, both things are possible through hacks, but the real answer is: DON'T DO IT.

Alexander
There several adaptions need to be done for my particular older codes that are still ansi to be worked in Unicode. The meaning of string equal to UnicodeString in Delphi 2009 doesn't mean it guarantees my older codes will work flawlessly in Delphi 2009. Because of this. I don't know how to explain my thought further, but that is one of my reason to start use explicit UnicodeString for now.
Phantom
And I need help from compiler to warning me.
Phantom
I don't understand what your situation is, but I feel that you're doing something very wrong. There ARE compiler warnings for string misuses.
Alexander
+1  A: 

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 string itself, 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 NOT DECLARED(UnicodeString)}
type UnicodeString = WideString;
{$IFEND}

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 :

{$IFDEF MY_APP_USE_UNICODE}
type AppString = UnicodeString;
{$ELSE}
type AppString = AnsiString;
{$ENDIF}

.. and work with that in your own code.

PatrickvL
@PatrickvL: Thanks, I highly appreciate your help. :-)
Phantom