• Advertisement
Sign in to follow this  

Checking Data Type in VB.Net

This topic is 3985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would i check if a text box's text is an inter ger , and if it is not an integer , than how can i get it to tell me this i need this type if textbox1.text = integer then Messagebox.show ( " this is an integer") else Messagebox.show("This isnt an integer") end if Obviously this does not work.

Share this post


Link to post
Share on other sites
Advertisement
You could use Int32.TryParse(), like this:

Dim OutVal As Integer

If Integer.TryParse(TextBox1.Text, OutVal) Then
MessageBox.Show(String.Format("This is an integer, and its value is {0}.", OutVal))
Else
MessageBox.Show("This isn't an integer.")
End If

Share this post


Link to post
Share on other sites
Moved to For Beginners. Pyromaniac, please make an effort to start your threads in the right forum to begin with.

Share this post


Link to post
Share on other sites

Module Module1

Sub Main()
If GetType(Integer) Is GetType(Char) Then
Console.WriteLine("1. An integer is an integer")
End If

If GetType(Integer) Is GetType(Integer) Then
Console.WriteLine("2. An integer is an integer")
End If

Dim x As Integer

If x.GetType() Is GetType(Integer) Then
Console.WriteLine("3. An integer is an integer")
End If


End Sub

End Module

Share this post


Link to post
Share on other sites
Thank you very much for you replies, they are a great help.
Btw , i will try to Put my posts in the right section next time.

What the program was , was a temperature conversion program, i needed the input to be an integer, then it would be passed and depending on which rdio button as checked it would convert it into that type. Any other methods, i want to know many.

Share this post


Link to post
Share on other sites
Btw, I found what i was looking for myself, If anyone want to know , it was the builtin "IsNumeric" Keyword .

If IsNumeric(Textbox1.text) then
Do stuff
End if

Share this post


Link to post
Share on other sites
I have a natural aversion to VB's built-in functions, as they are non-portable between other .NET-friendly languages. [smile]

Bear in mind that IsNumeric doesn't check for integers; it will also handle floating-point values.

Share this post


Link to post
Share on other sites
Quote:
Original post by pyromaniac4382
what exactly is 'IsNumeric' checking for then?


Things that are, well, numeric. As in, interpretable as some kind of number. (Or at least, one of the types of number that's built into the language. Some mathematicians seem to make a hobby of inventing new kinds of numbers ;) )

Share this post


Link to post
Share on other sites
So it is checking if the in put "IsNumeric", and it does include float points, is there any other ways out there to do this, say for character , would that one be IsChar?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement