How to get types and values โ€‹โ€‹of const array?

In my Delphi development, I want to pass a โ€œconst arrayโ€ (which may also contain a class) for the procedure, as well as in the loop procedure for the elements, and determine the type of element, as shown below.

Procedure Test(const Args : array of const); begin end; and in my code call it with some variables Procedure Test(); begin cls := TMyObject.create; i := 123; j := 'book'; l := False; Test([i,j,l, cls, 37.8]) end; 

How to loop on sent elements of an array and determine its type?

+7
source share
2 answers
 for I := Low(Args) to High(Args) do case TVarRec(Args[I]).VType of vtInteger: ... end; 
+7
source

Assuming you are using Unicode Delphi (otherwise you need to change the string case):

 procedure test(const args: array of const); var i: Integer; begin for i := low(args) to high(args) do case args[i].VType of vtInteger: ShowMessage(IntToStr(args[i].VInteger)); vtUnicodeString: ShowMessage(string(args[i].VUnicodeString)); vtBoolean: ShowMessage(BoolToStr(args[i].VBoolean, true)); vtExtended: ShowMessage(FloatToStr(args[i].VExtended^)); vtObject: ShowMessage(TForm(args[i].VObject).Caption); // and so on end; end; procedure TForm4.FormCreate(Sender: TObject); begin test(['alpha', 5, true, Pi, Self]); end; 
+17
source

All Articles