best regards,
Fabian
Fabian, How about a code sample of what you are
trying to do?
> Variant allows 4 or 8 byte floating point values while
> TVarRec only accepts the 10 byte extended type.
Only accepts extended? I thought that if it accepted
extended, then it could accept single and double, too.
> Apart from that: Is there an "official" conversion
> routine between the var* (varString, varSingle,..)
> and the vt* (vtChar, vtExtended,..) constants?
Official will not mean much unless all code is
forced to use it. --JohnH
procedure THardDisplay.HandleNeu(Sender: TObject);
var AStr: string;
AEffektive: array of TVarRec;
ABuffer: array of variant;
AWertD: TWertDatum;
i: integer;
begin
inherited HandleNeu(Sender);
SetLength(AEffektive, length(FWertIndizes));
for i := High(FWertIndizes) downto Low(FWertIndizes) do
AEffektive[i] := VariantToVarRec(Werte[i]);
AWertD.Wert := Format(FormatStr, AEffektive);
AWertD.Typ := wtDisText;
MyHardware.Einheit[Einheit].Werte[0] := AWertD;
end;
For simplification: What I get is an array of variants. What I want to get
out is a formated string, which is formated following a formatting rule
given in FormatStr, but the Format function only accepty arrays of TVarRec
(=array of const). But TVarRec uses Delphi types while variant and namely
TVarData uses OLE types.
The VariantToVarRec function which you can find in the code is what I am
looking after. For now I have coded it myself, but only for a small range of
data types and with a lot of overkill memory buffer (using dynamic arrays of
every type to keep track of all the memory), because TVarRec only keeps
Pointers for most types.
Here is the VariantToVarRec function:
function VariantToVarRec(AInput: variant): TVarRec;
var ATyp: integer;
begin
ATyp := VarType(AInput);
case ATyp of
varSmallint, varInteger, varByte:
begin
result.VType := vtInteger;
result.VInteger := AInput;
end;
varSingle, varDouble:
begin
result.VType := vtExtended;
SetLength(AExt, length(AExt)+1);
AExt[length(AExt)-1] := AInput;
result.VExtended := @AExt[length(AExt)-1];
end;
varCurrency:
begin
result.VType := vtCurrency;
SetLength(ACur, length(ACur)+1);
ACur[length(ACur)-1] := AInput;
result.VExtended := @ACur[length(ACur)-1];
end;
varBoolean:
begin
result.VType := vtBoolean;
SetLength(ABol, length(ABol)+1);
ABol[length(ABol)-1] := AInput;
result.VExtended := @ABol[length(ABol)-1];
end;
varString:
begin
result.VType := vtString;
SetLength(AStr, length(AStr)+1);
AStr[length(AStr)-1] := AInput;
result.VExtended := @AStr[length(AStr)-1];
end;
end;
end;
Fabian, Thanks; now I tiink that I understand your problem.
All I can suggest right now is a Google Groups search:
http://groups.google.com/groups?q=delphi+Variant+TVarRec
Regards, JohnH