Types non valides: 'Array' et 'dynamic array'
Question
J'ai écrit une fonction qui me donne la longueur d'un tableau dynamique en le convertissant en chaîne et en lui demandant la longueur (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;
Dans mon programme principal, je lisais le texte en chaîne, le convertissais en tableau
procedure TForm1.Button2Click(Sender: TObject);
var i: integer;
begin
for i:=0 to length(sString) do
begin
cChar[i] := sString[i];
end;
end;
et faire:
ShowMessage(IntToStr(arraylength(cChar)));
Je reçois l'erreur indiquée dans le titre.
La solution
Lorsque vous passez des tableaux à des procédures et à des fonctions dans Delphi, vous devez les déclarer en tant que type séparé. Ainsi:
type
MyArray = array of char;
et ensuite
function arraylength(a: MyArray ): integer;
BTW: pourquoi n'utilisez-vous pas des fonctions intégrées telles que Length ()? Dans Delphi2009, la chaîne de caractères est une chaîne Unicode. Ainsi, Longueur renvoie Longueur en caractères, pas en octets.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow