How to convert variant value into integer?
Asked Answered
S

4

15

Below is my code:

var
  i : integer;
  ...
  ...
if not VarIsNull(TcxLookupComboBox(Sender).EditValue) then
begin
  i := Integer(TcxLookupComboBox(Sender).EditValue);
end;

I can use VarToStr to convert variant into string but there is no VarToInt in Delphi for this. So, I have converted it like this Integer(TcxLookupComboBox(Sender).EditValue). Is this the right approach?

Stepmother answered 26/5, 2014 at 6:43 Comment(8)
Just assign it as i := TcxLookupComboBox(Sender).EditValue. Compiler will do the conversion for you.Abnegate
You should read about Variants before using them.Dehart
What type of value is in EditValue?Kaine
+1 no need to downvote imho. I guess EditValue is a variant? (if not, it does not make sence to use variants to convert to integer).Porush
@Porush - Yes, EditValue is variant.Stepmother
@TLama: you could make that an answer, maybe with a link to the documentation explaining variantsPorush
@nkp Yes, but what type of value is in the variant?Kaine
@jpfollenius, nkp The point of David's question is that if EditValue contains 'abc' you're not going to be able to convert it to an integer. However, if it contains 123 then simple assignment as indicated in TLama's comment is all that's needed.Shamblin
F
15

Have a look at this: http://docwiki.embarcadero.com/RADStudio/Rio/en/Variant_Types

Specifically check the Variant Type Conversions section.

You should be able to assign is directly using implicit type casting. As in Delphi just handles it for you.

As an example:

var
  theVar: Variant;
  theInt: integer;
begin

  theVar := '123';
  theInt := theVar;
  showmessage(IntToStr(theint));
end;

This works without issue.

To ensure that your data is an integer and that it is safe to do at runtime (as in to make use that you didn't have a string value in the variant, which would result in a runtime error) then have a look at the Val function: http://docwiki.embarcadero.com/Libraries/Rio/en/System.Val

Hope this helps.

Frit answered 26/5, 2014 at 12:14 Comment(2)
TryStrToInt is what should be used rather than ValKaine
For the sake of code clarity it sometimes is not desirable to use auxiliary integer variable to cast variant to integer.Woman
A
9

This might help:

function VarToInt(const AVariant: Variant): integer;
begin
  Result := StrToIntDef(Trim(VarToStr(AVariant)), 0);
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
  ShowMessage(IntToStr(VarToInt(NULL)));
  ShowMessage(IntToStr(VarToInt(' 124   ')));
  ShowMessage(IntToStr(VarToInt(13.87)));
  ShowMessage(IntToStr(VarToInt('Edijs')));
end;

Results are: 0, 124, 0 and 0. You can make it to work with floats tho.

Anatomist answered 8/6, 2014 at 14:48 Comment(1)
Is seems a little overkill to cast variant to string to check if it can be casted to integer.Woman
W
0

AFAIK Delphi lacks of simple function like this below:

function VarToIntDef(const V: Variant; const ADefault: Integer = 0): Integer;
begin
  if V = NULL then
    Result := ADefault
  else
    Result := V;
end;
Woman answered 22/12, 2022 at 15:3 Comment(0)
F
-1

I found a diferent soluction. If You are right that variant value is an integer value, You can use this:

//Using aggregate field in TClientDataSet
StrToInt(VarToStr(ClientDataSet.FieldByName('FieldName').AsVariant))
//Using a variant variable type
StrToInt(VarToStr(YourVariableName))

Is more easy

Flatworm answered 8/5 at 23:28 Comment(7)
You don't have to convert a variant to a string to get an int. You don't have to convert a variant at all - it's done automatically. If i is an integer, and you have the variant var1 := '123';, then i := var1; will do the conversion. All of your StrToInt(VarToStr()) is just utter nonsense. Your first line of code could be i := ClientDataSet.FieldByName('FieldName').AsInteger;, and the second could be i := YourVariableName;` if YourVarName is a variant. The automatic conversion of variants to other data types is the exact reason you use variants at all.Assembled
I got this error: [dcc32 Error] FImportacaoXML.pas(113): E2003 Undeclared identifier: 'VarToInt'Flatworm
There is no VarToInt in the code I provided. If you have an issue, ask a new question about that issue using the Ask Question button at the top right of this page. When you ask, make sure to include tags for the specific Delphi version you're using, and provide a minimal reproducible example that demonstrates the issue. The code I've posted has been used for years and years in my applications, starting with Delphi 2 (when TClientDataSet was introduced) and works fine.Assembled
I just presented a new idea. But I noticed that you are not very adept at this. Since it has been using the same code since Delphi 2. I use Alexandria. I didn't ask a new question, I just presented a new idea. And remember: a new idea that matches yours doesn’t mean it’s wrong. This is being uncompromising. I think you can be more polite in your answers. Thanks!Flatworm
Just an addendum: My code also works.Flatworm
ClientDataSet.FieldByName('FieldName').AsInteger This does not work in the aggregate field. I use my code is this case.Flatworm
You didn't say it has VarToInt, it was Embarcadero: [link]docwiki.embarcadero.com/Libraries/Alexandria/en/…Flatworm

© 2022 - 2024 — McMap. All rights reserved.