Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Explode aus CodeLib. - Inkompatible Typen. (https://www.delphipraxis.net/138839-explode-aus-codelib-inkompatible-typen.html)

Deltachaos 18. Aug 2009 12:28


Explode aus CodeLib. - Inkompatible Typen.
 
Ich habe ein Array of string als Ziel definiert und obwohl Explode als rückgabe doch auch ein array of String hat bekomm ich die meldung inkompatible typen.
Warum das?

Delphi-Quellcode:
type TSTringdynarray = array of String;

function Explode(const Separator, S: string; Limit: Integer = 0): TStringDynArray;
var
  SepLen: Integer;
  F, P: PChar;
  ALen, Index: Integer;
begin
  SetLength(Result, 0);
  if (S = '') or (Limit < 0) then Exit;
  if Separator = '' then
  begin
    SetLength(Result, 1);
    Result[0] := S;
    Exit;
  end;
  SepLen := Length(Separator);
  ALen := Limit;
  SetLength(Result, ALen);

  Index := 0;
  P := PChar(S);
  while P^ <> #0 do
  begin
    F := P;
    P := AnsiStrPos(P, PChar(Separator));
    if (P = nil) or ((Limit > 0) and (Index = Limit - 1)) then P := StrEnd(F);
    if Index >= ALen then
    begin
      if aLen=0  then aLen := 10 else

      aLen := aLen * 2;
      SetLength(Result, ALen);
    end;
    SetString(Result[Index], F, P - F);
    Inc(Index);
    if P^ <> #0 then Inc(P, SepLen);
  end;
  if Index < ALen then SetLength(Result, Index);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ar: array of string;
begin
   ar := explode('|', 'bbb|ccc|ddd')
end;

himitsu 18. Aug 2009 12:29

Re: Explode aus CodeLib. - Inkompatible Typen.
 
das liegt daran, daß zwar der Aufbau der Typen zwar gleich ist, aber dennoch die Typen nicht identisch sind!

Delphi ist da sehr streng
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  ar: TStringDynArray;
begin
   ar := explode('|', 'bbb|ccc|ddd')
end;

mkinzler 18. Aug 2009 12:30

Re: Explode aus CodeLib. - Inkompatible Typen.
 
In welcher Zeile tritt der Fehler auf?

Tyrael Y. 18. Aug 2009 12:31

Re: Explode aus CodeLib. - Inkompatible Typen.
 
Wozu hast du es neu deklariert?

TStringDynArray ist in types bereits vorhanden.
Binde types einfach mal ein.

Deltachaos 18. Aug 2009 12:41

Re: Explode aus CodeLib. - Inkompatible Typen.
 
thx :)
nur wie convertiere ich das wieder?
z.b. wenn ich den inhalt in eine listbox übertragen will?
oder muss ich das überhaupt?

Klaus01 18. Aug 2009 12:45

Re: Explode aus CodeLib. - Inkompatible Typen.
 
sollte in etwa so funktionieren:

Delphi-Quellcode:
for i:=low(ar) to high(ar) do
  listbox.items.add(ar[i]);
Grüße
Klaus

DeddyH 18. Aug 2009 12:49

Re: Explode aus CodeLib. - Inkompatible Typen.
 
Klaus war zwar schneller, aber ich poste meine gerade erstellte Routine trotzdem.
Delphi-Quellcode:
procedure ArrayToTStrings(const Arr: TStringDynArray; const s: TStrings);
var i: integer;
begin
  s.BeginUpdate;
  try
    s.Clear;
    for I := Low(arr) to High(Arr) do
      s.Add(Arr[i]);
  finally
    s.EndUpdate;
  end;
end;
[edit] Eine Zeile verrutscht [/edit]

Deltachaos 18. Aug 2009 12:50

Re: Explode aus CodeLib. - Inkompatible Typen.
 
ok also muss ich das array durchgehen


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:08 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz