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?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
  • 4
    Just assign it as `i := TcxLookupComboBox(Sender).EditValue`. Compiler will do the conversion for you. – TLama May 26 '14 at 06:46
  • You should read about Variants before using them. – Kromster May 26 '14 at 07:16
  • 1
    What type of value is in EditValue? – David Heffernan May 26 '14 at 07:23
  • +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). – jpfollenius May 26 '14 at 07:54
  • @jpfollenius - Yes, EditValue is variant. –  May 26 '14 at 09:27
  • @TLama: you could make that an answer, maybe with a link to the documentation explaining variants – jpfollenius May 26 '14 at 12:10
  • @nkp Yes, but what type of value is in the variant? – David Heffernan May 26 '14 at 17:38
  • 2
    @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](http://stackoverflow.com/questions/23864212/how-to-convert-variant-value-into-integer#comment36731048_23864212) is all that's needed. – Disillusioned May 26 '14 at 20:46

3 Answers3

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.

Shaun Roselt
  • 1,650
  • 5
  • 18
  • 44
EchelonKnight
  • 321
  • 2
  • 4
10

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.

Edijs Kolesnikovičs
  • 1,627
  • 3
  • 18
  • 34
  • Is seems a little overkill to cast variant to string to check if it can be casted to integer. – m227 Dec 22 '22 at 14:58
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;
m227
  • 51
  • 5