Another easy one!

This is already my 3rd easy Q regarding Delphi in the past 3 days.  For those who didn't get to see the first two, know that I haven't done any Delphi in a long time so please be easy on the laughing!

Is there some kind of equivalent function of the VB6 IsNumeric in Delphi ?  For those who aren't familiar with the VB function, know that it returns a boolean value depending on if the variable is numeric or not.

--
'VB example:
dim myVar as integer
myVar = 10
if IsNumeric(myVar) then doSomeThing
--

I just want to validate an entry from an editbox.  I don't want to try searches in a database if the entry isn't numeric.

Any hint ??
Thanks!
qasAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
there are more possibilites

first

  try
     intvar := IntToStr(edit1.text);
  except
     showmessage('No Integer');
  end;

second
  use the val-procedure->see delphi online help

maybe more possibilities,

keep in mind, that you can expand [object] pascal
with own functions

meikl ;-)
0
 
kretzschmarCommented:
sample

function isnumeric(s : String) : boolean;
var
   dummyInt : Integer;
begin
  result := true;
  try
   dummyInt := StrToInt(s);
  except
    result := false;
  end;
end;

meikl ;-)      
0
 
Russell LibbySoftware Engineer, Advisory Commented:

Another possibility...

function IsNumeric(const Value: Variant): Boolean;
begin

  try
     VarAsType(Value, varDouble);
     result:=True;
  except
     result:=False;
  end;

end;

Would allow you to pass pretty much anything
ie:

  IsNumeric(3.455);
  IsNumeric(12);
  IsNumeric(Edit1.Text);
  IsNumeric('23');


If you only want to validate for integers, change the VarAsType to

VarAsType(Value, varInteger);

-------
Russell
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
DrDelphiCommented:
VarAsType is not going to work. Example:

function IsNumeric(const Value: Variant): Boolean;
begin
 try
    VarAsType(Value, varString);
    result:=True;
 except
    result:=False;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var num:smallint;
begin
 num:=8;
 if isnumeric(num) then showmessage('True');
end;



Shows "True".


StrToInt is a safer bet, although you could do something like this also:

function IsNumeric(const Value: String): Boolean;
var Num:set of char;
    Loopint:integer;
begin
    Num:=['0'..'9'];
    For loopint:=1 to Length(value) do
    begin
      if (not (Value[loopint] in Num)) then
      begin
         Result:=False; /// no use going on
         exit;// so get out.
      end;
     Result:=True;/// got this far, so it's good.
    end;
end;



Good luck!!


0
 
DrDelphiCommented:
The advantage of my code versus StrToInt is that it won't raise an exception, even in the IDE. It is also more partable in terms of integer types. StrToInt is limited to I believe cardinal before you have to use StrToInt64.


Good luck!!



0
 
Russell LibbySoftware Engineer, Advisory Commented:

DrDelphi,

I'm not sure I understand why VarAsType is not going to work. I did not use "VarAsType(Value, varString)" as you did, but "VarAsType(Value, varDouble)", which falls under the variant rules of conversion as follows:

--------------
integer->real
real->real
string->real (using windows regional settings)
char(same as string)->real
boolean->real (0, -1)
unassigned->real (0)
--------------

If the value cannot be changed to a double value, then an exception is raised and false is returned.




0
 
SteveWaiteCommented:
Edit box is just a string so..
Returns true for numeric strings such as 4, 7.03 and 2.2E-15

function IsNumeric(S: String): Boolean;
begin
  try
    StrToFloat(S);
    Result := True;
  except
    Result := False;
  end;
end;

Regards,
Steve
0
 
RabsterCommented:
I always use the val procedure as mentioned by kretzschmar as a second option. It's easy and does not generate errors.

Var
  I,Code : Integer;
begin
  Val(Edit1.Text,I,Code);
  If Code = 0 then
  ShowMessage(Edit1.Text + ' is numerical')
  else
  ShowMessage(Edit1.Text + ' is not numerical,             character '+ IntToStr(Code) + ' is not numerical.');
end;
0
 
qasAuthor Commented:
The val stuff worked perfectly!  
I liked the idea to not raise exceptions.

Thanks for all the answers!
0
 
SteveWaiteCommented:
Oh dear.. The idea IS to raise exceptions, doh!!
Hence the exceptions raised by standard functions like StrToInt(). (and StrToFloat)
Therefor you just joined the bicycle race on a pogo stick, oops.
0
 
SteveWaiteCommented:
Actually, reading that back sounds terrible, sorry everyone. I don't mean you personally gas. That's the last bit of toungue-in-cheek I type. Agreed Val() is perfect when you need the position in the string as well.

It's just that there's nothing to worry about with exceptions. It's easy to make the mistake of coding around them when new to Delphi. They should be used accordingly rather than ignored.

Regards,
Steve


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.