Question

I wrote a function that gives me the length of a dynamic array by converting it to string and asking length(trim(string));

function arraylength(a: array of char): integer;
var i: integer;
s: string;
begin
    for i:=0 to high(a) do
  begin
  s[i] := a[i-1];
    Result := length(trim(s));
  end;
end;

In my main program i read text into a string, convert it to array

procedure TForm1.Button2Click(Sender: TObject);
var i: integer;
begin
  for i:=0 to length(sString) do
  begin
    cChar[i] := sString[i];
  end;
end;

and do:

ShowMessage(IntToStr(arraylength(cChar)));

I get the error as stated in the title.

Was it helpful?

Solution

When passing arrays to procedures and functions in delphi you should declare them as a separate type. Thus:

type
  MyArray =  array of char; 

and then

function arraylength(a: MyArray ): integer;

BTW: why aren't you using built-in functions like Length() ? In Delphi2009 type string is unicode string, so Length returns Length in characters, not in bytes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top