Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Uso de variables tipo Variant (https://www.clubdelphi.com/foros/showthread.php?t=36810)

robinsongm 25-10-2006 09:53:03

Uso de variables tipo Variant
 
Saludos,

Tengo una función similar a :

NameFunction(Params: Array of Variant): String;

¿Cómo puedo saber la cantidad de parámetros que me han pasado?

¿Cómo sé de que tipo especifico es cada elemento del array, para luego convertirlo a string?

El caso es que quiero crear una cadena parecida a: 'Valor1, Valor2, Valor3, Valor4....ValorN' de tipo string. En principio los valores posibles en los parámetros serían primitivos (boolean, integer, int64, string, bye...).
Bien sé que podría solicitar Params como Array of String, pero parte de la funcionalidad de la función, valga la redundancia, se perdería.

¿Como hacer esto?

GRACIAS!!!

dec 25-10-2006 12:15:26

Hola,

Ignoro si esta es una forma muy elegante de hacerlo, pero, parece funcionar...

Código Delphi [-]
function Linda(parametros: array of Variant) : string;
var
  i: integer;
begin
  for i := 0 to Length(parametros)-1 do
    ShowMessage( string(parametros[i]) );
end;

// Ejemplo de llamada a Linda
Linda(['Cadena', 1, 0.1, false]);

Incluso parece que el "cast" a "string" sobraría, puesto que parece que el compilador se encarga de ello. De hecho lo siguiente funciona exactamente igual que lo anterior:

Código Delphi [-]
function Linda(parametros: array of Variant) : string;
var
  i: integer;
begin
  for i := 0 to Length(parametros)-1 do
    ShowMessage( parametros[i] );
end;

robinsongm 25-10-2006 13:35:59

Muchas Gracias!
 
Al menos para lo que quiero me vale!

Gracias !!

Enan0 25-10-2006 13:36:56

Hola Delphi trae toda una coleccion de funciones para manejar eso:

COn
Cita:

function VarArrayDimCount(const A: Variant): Integer;

Description

VarArrayDimCount returns the number of dimensions of the given Variant. The resulting value is zero if the given Variant is not a Variant array.

TE devuelve la Dimiencion del Array
y con.

Cita:

function VarArrayHighBound(const A: Variant; Dim: Integer): Integer;

Description

VarArrayHighBound returns the high bound of the given dimension in the given Variant array. The Dim parameter should be 1 for the leftmost dimension, 2 for the second leftmost dimension, and so on. An EVariantError exception is raised if the Variant given by A is not an array, or if the dimension specified by Dim does not exist.
La Longitud
luego deberias aplicar un FOR como de dijo dec

Saludos


La franja horaria es GMT +2. Ahora son las 05:43:34.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi