Identify the type of variable received in a generic function

Asked

Viewed 896 times

2

I have a function in Delphi that receives a kind of generic data, I would like to know what kind of data received, like this:

function MinhaFuncao<T>(): T

I need to know what kind is in <T>.

  • not yet, (I already got it using RTTI), I have a query form Enerico: when I prompt you I pass a type <T> to it, is this <T> that tells me which class it has to mainupular in this query and when the user selects an item on the grid I have to return an instance of this populated class, example step to it a Tpessoa and when the

1 answer

1

To know the type of variable we have in the function you can use the following procedure, example:

procedure TForm1.ShowBasicVariantType(varVar: Variant);
var typeString: string;
   basicType : Integer;
begin
  basicType := VarType(varVar) and VarTypeMask;

  case basicType of
    varEmpty     : typeString := 'varEmpty';
    varNull      : typeString := 'varNull';
    varSmallInt  : typeString := 'varSmallInt';
    varInteger   : typeString := 'varInteger';
    varSingle    : typeString := 'varSingle';
    varDouble    : typeString := 'varDouble';
    varCurrency  : typeString := 'varCurrency';
    varDate      : typeString := 'varDate';
    varOleStr    : typeString := 'varOleStr';
    varDispatch  : typeString := 'varDispatch';
    varError     : typeString := 'varError';
    varBoolean   : typeString := 'varBoolean';
    varVariant   : typeString := 'varVariant';
    varUnknown   : typeString := 'varUnknown';
    varByte      : typeString := 'varByte';
    varWord      : typeString := 'varWord';
    varLongWord  : typeString := 'varLongWord';
    varInt64     : typeString := 'varInt64';
    varStrArg    : typeString := 'varStrArg';
    varString    : typeString := 'varString';
    varAny       : typeString := 'varAny';
    varTypeMask  : typeString := 'varTypeMask';
  end;

  ShowMessage('Variant type is '+typeString);
end;

Code to call the function:

var myVar : Variant;
begin
  ShowMessage('Variant value = not yet set');
  ShowBasicVariantType(myVar);

  myVar := 123;
  ShowMessage('Variant value = 123');
  ShowBasicVariantType(myVar);

  myVar := myVar + 456;
  ShowMessage('Variant value = 123 + 456');
  ShowBasicVariantType(myVar);

  myVar := 'String '+IntToStr(myVar);
  ShowMessage('Variant value = String 579');
  ShowBasicVariantType(myVar);
end;

Result:

Variant value = not yet set
Variant type = varEmpty
Variant value = 123
Variant type = varByte
Variant value = 123 + 456
Variant type = varInt64
Variant value = String 579
Variant type = varString

For more information see Delphibasics, can still see here has one more possible example.

  • Thank you, for the return! I don’t know if I could explain, but it’s like this: Minhafuncao<T> T

  • @Lucianoferreira only needs to change the Procedure to Function and in the first line of your Minhafuncao the function

  • Minhafuncao<T>(): T can be a Tpessoa, Tproduto, Tmarca, this treatment also serves to identify these types of classes, currently I am using RTTI,

  • like this: "if T is Tpessoa then ..."

Browser other questions tagged

You are not signed in. Login or sign up in order to post.