I have very old code (from D3):
TMyRecord = record
Index : Integer;
Header : String[70];
Strings : Array[1..MAX_VALUES] of String[70];
end;
TMyClass = class(TComponent)
FData : ^TMyRecord;
...
end;
constructor TMyClass.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
New(FData);
...
end;
destructor TMyClass.Destroy;
begin
Dispose(FData);
inherited;
end;
Q: Is it safe to replace String[70]
with WideString;
and Array[1..MAX_VALUES] of String[70]
with Array[1..MAX_VALUES] of WideString
? (Please explain why)
I need this to support Unicode in Delphi 7.
In general you should never use Widestring. It is only meant for compatibility with COM BSTR.
However you are using a pre-2009 version so if you need Unicode you don't have a choice.
WideString is dynamically allocated, when you new
the record Delphi will add code to initialize your strings.
You don't have to initialize them yourself.
Just like shortstrings WideStrings are not reference counted, but they will be destroyed when you dispose
the record.
If you assign the Widestring to another Widestring Delphi will make a copy, this is slightly less efficient than refcounting, but otherwise not a problem.
Whenever a Widestring goes out of scope it is destroyed.
Be careful with PWideChar, these will be dangling when a WideString is destroyed.
VCL cannot display WideString
Note that whilst Delphi 7 does have support for Unicode with Widestring the VCL cannot display your Widestrings, it can only display AnsiString.
If you want to display WideStrings use the TNT components, see this answer for more info: Handling a Unicode String in Delphi Versions <= 2007
If you are going to assign WideString to (Ansi)string you might as well use plain string, because you'll lose all your unicode.
You can use UTF8, but D7 cannot display UTF8 either.
Caveat: indexing in Asian locales
A further caveat is that MyWidestring[i]
does not necessarily mean the ith character because Unicode cannot be fully expressed in 2 bytes per char.
Unless you're using an asian language this should not affect you.
Q: Is it safe to replace String[70] with WideString;
Yes, but it is easier to replace String[70]
with String
(aka AnsiString). Because the D7 VCL supports AnsiString but not WideString.
Other than that you don't really have a problem.
Further reading
https://grahamwideman.wikispaces.com/Delphi+String+Types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With