Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert textbox input to integer 1

Status
Not open for further replies.

mancroft

Programmer
Oct 26, 2002
267
GB
Convert textbox input to integer

Hello

I am using Visual Basic Express 2005 Beta.

Let's say you have a textbox into which a number e.g. 87884 is typed.

What is the best way to convert that number to an integer?

Thank you.
 
CInt(Me.TextBoxName.Text)

If at first you don't succeed, call in an airstrike. - Murphy's Laws of Combat Operations
 
'Really you should check to see if an int was entered in the first place.

dim i as integer
Try
i = integer.parse(textbox.text)
Catch
'Just incase something fails. If you checked first then this won't fail. You could set an error message here.
i = 0
end try
 
No need for a try-catch loop.Just use the IsNumeric function
Code:
dim i as Integer=0
if isnumeric(textbox.text) then i=cint(textbox.text)


Sweep
...if it works dont mess with it
 
The reason I use try catch is because isnumeric is a sugar coating vb function and under the covers does the same thing as the try catch method. Having used VB and C# for 3 years can tell you that VB has major issues in large applications and I don't want to perpetuate it's use. Even with simple functions like this.

Also, your app should handle entering 0 and "asdf" differently, ie tell the user they screwed up.

Still I can see why IsNumeric is attractive.
 
If the textbox should only accept numerics my position is that it should not allow any non-numeric to be entered. For this I like to make a simple control inherited from textbox with a property that allows the developer to select what type of input to allow such as integer, decimal, etc. You can get as fancy or keep it as simple as you want. You could also just use the various key events to disallow non-numerics.

I don't like when a program allows me to enter characters in a field that are not allowed only to tell me later that they weren't allowed.
 
I'm offended by stsuing's comments about VB6. I've created a large VB6 app without any major issues. Of course that's a discussion for another thread, on another day...

Stsuing does make a good point regarding the try/catch method vs. the isnumeric method. The original poster wants integers, not 'numerics'.

This isnumeric function allows for some strange behavior that must be accomodated in order to use it properly.

The following table highlights this.

Code:
Value    IsNumeric    Integer.Parse
100         True        No Error
-50         True        No Error
10.4        True        Error
e4          False       Error
2e4         True        Error

The e represents scientific notation for numbers and is, therefore, a valid number.

In VB6, it's even worse. You can also use the 'd' character to represent scientific notation.

As you can see, the Integer.Parse method is more stringent which is probably what the original poster had in mind.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top