Solved

Another easy one!

Posted on 2002-03-13
11
675 Views
Last Modified: 2012-08-13
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!
0
Comment
Question by:qas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 6861931
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6861946
sample

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

meikl ;-)      
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 6861950

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:DrDelphi
ID: 6862399
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
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6862413
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 6862519

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
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6863229
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
 

Expert Comment

by:Rabster
ID: 6864528
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
 

Author Comment

by:qas
ID: 6864691
The val stuff worked perfectly!  
I liked the idea to not raise exceptions.

Thanks for all the answers!
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6864926
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
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6866285
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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question