Search code examples
delphidelphi-7

Object or class type required in delphi7


I try to convert string value to char array. I write below code but get error that

[Error] Unit1.pas(35): Record, object or class type required

on this line section a := s.ToCharArray;

procedure TForm1.Button1Click(Sender: TObject);
 var
 a: array of char;
 s: String;
begin
 s:= Edit1.Text;
 a := s.ToCharArray;
end;

What is problem?


Solution

  • In Delphi 7 there is no string helper, hence the compilation error you encounter. Use this instead

    SetLength(a, Length(s));
    Move(Pointer(s)^, Pointer(a)^, Length(s) * SizeOf(s[1]));
    

    Now, SizeOf(s[1]) is 1 in Delphi 7 so the multiplication isn't strictly necessary, but it does mean that your code will work correctly if ever you move to a Unicode Delphi.

    Or, if the use of Move isn't to your taste, use a loop to copy the characters

    SetLength(a, Length(s));
    for i := 1 to Length(s) do
      a[i - 1] := s[i];