Checking Data Type in VB.Net

Started by
8 comments, last by pyromaniac4382 17 years ago
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.
Advertisement
You could use Int32.TryParse(), like this:

Dim OutVal As IntegerIf 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

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Moved to For Beginners. Pyromaniac, please make an effort to start your threads in the right forum to begin with.
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 SubEnd Module

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

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.
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

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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

what exactly is 'IsNumeric' checking for then?
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 ;) )
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?

This topic is closed to new replies.

Advertisement