delphi-2009

How can I create an Delphi object from a class reference and ensure constructor execution?

假如想象 提交于 2019-11-26 13:03:54
问题 How can I create an instance of an object using a class reference, and ensure that the constructor is executed? In this code example, the constructor of TMyClass will not be called: type TMyClass = class(TObject) MyStrings: TStrings; constructor Create; virtual; end; constructor TMyClass.Create; begin MyStrings := TStringList.Create; end; procedure Test; var Clazz: TClass; Instance: TObject; begin Clazz := TMyClass; Instance := Clazz.Create; end; 回答1: Use this: type TMyClass = class(TObject)

How Can I Best Guess the Encoding when the BOM (Byte Order Mark) is Missing?

那年仲夏 提交于 2019-11-26 10:29:21
问题 My program has to read files that use various encodings. They may be ANSI, UTF-8 or UTF-16 (big or little endian). When the BOM (Byte Order Mark) is there, I have no problem. I know if the file is UTF-8 or UTF-16 BE or LE. I wanted to assume when there was no BOM that the file was ANSI. But I have found that the files I am dealing with often are missing their BOM. Therefore no BOM may mean that the file is ANSI, UTF-8, UTF-16 BE or LE. When the file has no BOM, what would be the best way to

Converting TMemoryStream to 'String' in Delphi 2009

社会主义新天地 提交于 2019-11-26 08:08:46
问题 We had the following code prior to Delphi 2009: function MemoryStreamToString(M : TMemoryStream): String; var NewCapacity: Longint; begin if (M.Size = > 0) or (M.Memory = nil) then Result:= \'\' else begin if TMemoryStreamProtected(M).Capacity = M.Size then begin NewCapacity:= M.Size+1; TMemoryStreamProtected(M).Realloc(NewCapacity); end; NullString(M.Memory^)[M.Size]:= #0; Result:= StrPas(M.Memory); end; end; How might we convert this code to support Unicode now with Delphi 2009? 回答1: The