Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Typ einzelner Array of const-Elemente (https://www.delphipraxis.net/83860-typ-einzelner-array-const-elemente.html)

inherited 7. Jan 2007 15:34


Typ einzelner Array of const-Elemente
 
Ich möchte dieses Thema an dieser Stelle noch einmal aufgreifen, da sich die Delphi-Hilfe darüber ausschweigt oder ich unfähig bin, ordentlich zu suchen. Auch hat die Forensuche mir hier nicht weitergeholfen.
Wie genau bekomme ich denn den Typ der einzelnen Parameter raus? Ich weiß wie ich einzelne davon typisiere, aber nicht, wie ich herausbekommen kann von was für einem typ es ist.

sirius 7. Jan 2007 16:05

Re: Typ einzelner Array of const-Elemente
 
Delphi-Quellcode:
function fstr(z:variant):string;
begin
   if (TVarData(z).VType = varinteger)
    or(TVarData(z).VType = varbyte)
    or(TVarData(z).VType = varword)
    or(TVarData(z).VType = varint64)
      then str(z:0:0,result)
      else str(z,result);
end;
(Ausschnitt aus einer Funktion (die wahrscheinlich in diesem Ausscheitt recht sinnlos geworden ist))

Mit nem array of const dürfte es genauso funktionieren.

inherited 7. Jan 2007 16:16

Re: Typ einzelner Array of const-Elemente
 
Leider nicht :cry:

So versuche ich es:

Delphi-Quellcode:
procedure Test(a: Array of Const);
var i: Integer;
begin
  for i := 0 to high(a) do
  begin
    if (TVarData(a[i]).VType=varinteger)
     or(TVarData(a[i]).VType = varbyte)
     or(TVarData(a[i]).VType = varword)
     or(TVarData(a[i]).VType = varint64) then
      ShowMessage(IntToStr(a[i]);
  end;
end;

3_of_8 7. Jan 2007 16:19

Re: Typ einzelner Array of const-Elemente
 
Natürlich geht das nicht, ein array of const ist schließlich kein array of Variant.

Ich denke mal, du kannst nur die Größe der einzelnen Array-Elemente feststellen und auf sie zugreifen.

thkerkmann 7. Jan 2007 16:22

Re: Typ einzelner Array of const-Elemente
 
Hi,

nee, so nicht, ist ja kein Variant array

aber auf der Borland TI Webseite findet man dies

How to use an array of const

Delphi-Quellcode:
procedure AddStuff( Const A: Array of Const );
Var i: Integer;
Begin
  For i:= Low(A) to High(A) Do
  With A[i] Do
    Case VType of
    vtExtended: Begin
       { add real number, all real formats are converted to
         extended automatically }
      End;
    vtInteger: Begin

       { add integer number, all integer formats are converted
         to LongInt automatically }
      End;
    vtObject: Begin
        If VObject Is DArray Then
          With DArray( VObject ) Do Begin
            { add array of doubles }
          End
        Else If VObject Is IArray Then
          With IArray( VObject ) Do Begin
            { add array of integers }
          End;
      End;
    End; { Case }
End; { AddStuff }
Gruss

inherited 7. Jan 2007 16:22

Re: Typ einzelner Array of const-Elemente
 
Zitat:

Zitat von 3_of_8
Natürlich geht das nicht, ein array of const ist schließlich kein array of Variant.

Nein, wirklich? -.-

€dit: Ich mach es jetzt mit Variants, danke dir ;)

sirius 7. Jan 2007 16:29

Re: Typ einzelner Array of const-Elemente
 
Du nimmst statt TVardata TVarRec.
etwa so:
Delphi-Quellcode:
for i:=0 to high(a) do begin
  if (TVarRec(a[i]).VType = vtinteger)
      then memo1.lines.Add(inttostr(TVarRec(a[i]).VInteger))
 end;
Edit:
Zitat:

Natürlich geht das nicht, ein array of const ist schließlich kein array of Variant.
Aber ein array of TVarRec

inherited 7. Jan 2007 18:29

Re: Typ einzelner Array of const-Elemente
 
Habe dein Edit erst jetzt gesehen, so klappt es auch wunderbar, danke!

sirius 7. Jan 2007 18:36

Re: Typ einzelner Array of const-Elemente
 
Zitat:

Zitat von inherited
Habe dein Edit erst jetzt gesehen, so klappt es auch wunderbar, danke!

Ja, ich muss immer erstmal gucken, wie das genau war. Der erste Post, war das erste was mir einfiel und als Idee gedacht, in welche Richtung man suchen könnte.

Übrigens:
Manchmal musst du schauen welchen Typ du überhaupt übergibst. Grade bei strings gibt es ja für vtype mehrere Varianten. Dann lass dir einfach mal testweise vtype ausgeben und schau, welchen Typ du übergeben hast.

SubData 7. Jan 2007 18:49

Re: Typ einzelner Array of const-Elemente
 
Ich hab mir vor einiger Weile mal eine Funktion geschrieben, die ein Array of Const "lesbar" macht.
Evtl. hilft dir das :)

Delphi-Quellcode:
class function TCODDBHelperClass.ArrayToString(Arr: Array of Const): String;
var
  Loop   : Integer;
  Value  : TVarRec;
begin
  Result := '[';
  for Loop := 1 to Length(Arr) do
  begin
    Value := Arr[Loop - 1];
    case Value.VType of
      vtInteger    : Result := Result + IntToStr(Value.VInteger);
      vtBoolean    : Result := Result + BoolToStr(Value.VBoolean);
      vtChar       : Result := Result + Value.VChar;
      vtExtended   : Result := Result + FloatToStr(Value.VExtended^); // '_EXTENDED_';
      vtString     : Result := Result + '''' + Value.VString^ + '''';
      vtPointer    : Result := Result + Format('%p', [Value.VPointer]);
      vtPChar      : Result := Result + '''' + Value.VPChar + '''';
      vtObject     : Result := Result + '_OBJECT_';
      vtAnsiString : Result := Result + '''' + String(Value.VAnsiString) + '''';
      vtCurrency   : Result := Result + CurrToStr(Value.VCurrency^); //'_CURRENCY_';
      vtVariant    : Result := Result + Value.VVariant^;
      vtWideString : Result := Result + '''' + WideString(Value.VWideString) + '''';
      vtInt64       : Result := Result + IntToStr(Int64(Value.VInt64));
    else
                      Result := Result + '_UNKNOWN_';
    end;
    if (Loop < Length(Arr)) then Result := Result + ', ';
  end;
  Result := Result + ']';
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:00 Uhr.
Seite 1 von 2  1 2      

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