Question "FormatException was unhandled"

Markissimo

New member
Joined
Oct 25, 2008
Messages
3
Programming Experience
Beginner
Hey guys, i'm very new on programming at VB.NET and Oriented Objects Programming.. So, i'm trying to make a simple program here and i got some problems and i realized that i don't know how to program by oriented objects =/

So.. I'm new in this community and i would like to know if some good soul would have time and patience to "fix" this program that i made, but got some errors and just programmed by Events

The program just compare the textboxes, for example txtSpeed1 with txtSpeed2, then it put a color on the labels depending on which one is bigger
So Here is the program:
RapidShare: Easy Filehosting

If nobody don't want to waste time with the full program by editing it, so please answer this:
I got an error in this If, it just say "FormatException was unhandled"
VB.NET:
   If (Convert.ToInt16(txtAccel1.Text)) < (Convert.ToInt16(txtAccel2.Text)) Then
            lblAccel1.ForeColor = Color.Red
            lblAccel2.ForeColor = Color.LimeGreen
            lblAccel1.Text = txtAccel1.Text
            lblAccel2.Text = txtAccel2.Text
        ElseIf (Convert.ToInt16(txtAccel1.Text) = Convert.ToInt16(txtAccel2.Text)) Then
            lblAccel1.ForeColor = Color.Black
            lblAccel2.ForeColor = Color.Black
            lblAccel1.Text = txtAccel1.Text
            lblAccel2.Text = txtAccel2.Text
        ElseIf (Convert.ToInt16(txtAccel1.Text) > Convert.ToInt16(txtAccel2.Text)) Then
            lblAccel1.ForeColor = Color.LimeGreen
            lblAccel2.ForeColor = Color.Red
            lblAccel1.Text = txtAccel1.Text
            lblAccel2.Text = txtAccel2.Text
        End If

I'm asking for it to be a better programmer as i'm young and started now with this language. Thanks since now
 
Last edited:
First off, welcome to the forum. Secondly, formatting errors are common, they're something you'll have no matter what language you're programming in (even non-OOP languages)

Based on the code you've posted, it sounds like the Convert.ToInt16() is falling over based on the text in the one of the textbox's, without knowing more about the error (a line number and/or a screen shot) it's hard to tell, the posted code certainly looks good from here.

Another approach to this would be to set up two integer variables, one for each Accel textbox then do the comparisons on the integer rather then converting them over and over for each comparison until it finds the right one. Here's my take on it:
VB.NET:
Dim Accel1 As Integer = Convert.ToInt32(txtAccel1.Text.Trim)
Dim Accel2 As Integer = Convert.ToInt32(txtAccel2.Text.Trim)

lblAccel1.Text = txtAccel1.Text.Trim
lblAccel2.Text = txtAccel2.Text.Trim

Select Case True
    Case Accel1 > Accel2
        lblAccel1.ForeColor = Color.LimeGreen
        lblAccel2.ForeColor = Color.Red
    Case Accel1 = Accel2
        lblAccel1.ForeColor = Color.Black
        lblAccel2.ForeColor = Color.Black
    Case Accel1 < Accel2
        lblAccel1.ForeColor = Color.Red
        lblAccel2.ForeColor = Color.LimeGreen
End Select
 
Hmmm, i got it and this error is over, but now i have another one... when i build the project i got this one:



I'm braziliam, and it's portuguese, the translation is:
"Error on create the form. Check Exception.InnerException to get details. The error is: Reference to object not defined to an object instance"
 
That's a common one "Object not set to an instance of an object" is the English wording of the exception. What it means is that you're trying to use an object when it doesn't exist in memory yet. You have the object (class) made and everything but you still need the instance of it to use it. To get an instance of it you use the 'New' keyword, for example:
VB.NET:
Dim MyForm As Form2
MyForm.Show()
The above will cause an error but below it wont:
VB.NET:
Dim MyForm As New Form2
MyForm.Show()
 
Hmm, understood, but where i put this?

note: i've updated the link to download the full program, if you download it, it should be easier to help me, i think =p
 
If it's used frequently then I declare the variable at the module level and simply make a new instance whenever needed. Otherwise I'll simply declare it and have it make a new instance as needed.
 
Back
Top