PDA

Ver la Versión Completa : Crear "Array of Const" programáticamente


dec
30-06-2006, 15:20:18
Hay funciones que toman parámetros del tipo "array of const". Quisiera saber como hacer una función de este tipo y sobre todo como crear un "array of const" mediante código. ¡¡No consigo declarar una variable como "array of const"!!

El siguiente código muestra como hacer ambas cosas.


function MakeStr(const Args: array of const): string;
const
BoolChars: array[Boolean] of Char = ('F', 'T');
var
I: Integer;
begin
Result := '';
for I := 0 to High(Args) do begin
with Args[I] do
case VType of
vtInteger: Result := Result + IntToStr(VInteger);
vtBoolean: Result := Result + BoolChars[VBoolean];
vtChar: Result := Result + VChar;
vtExtended: Result := Result + FloatToStr(VExtended^);
vtString: Result := Result + VString^;
vtPChar: Result := Result + VPChar;
vtObject: Result := Result + VObject.ClassName;
vtClass: Result := Result + VClass.ClassName;
vtAnsiString: Result := Result + string(VAnsiString);
vtCurrency: Result := Result + CurrToStr(VCurrency^);
vtVariant: Result := Result + string(VVariant^);
end;
REsult := Result + ' '
end;
end;

var
ElArray: array[ 0..2 ] of TVarRec;
Cadena: ShortString = 'Hola';

procedure TForm1.Button1Click(Sender: TObject);
begin
ElArray[ 0 ].VType := vtInteger;
ElArray[ 0 ].vInteger := 0;
ElArray[ 1 ].VType := vtBoolean;
ElArray[ 1 ].vBoolean := False;
ElArray[ 2 ].VType := vtString;
ElArray[ 2 ].vString := @Cadena;
Caption := MakeStr( ElArray );
end;