1

I am about to finish my college project, so I am just really eager to finish this! after apparently finishing all of the hard bit and running the program, errors began popping up. This is the one I can't fix so far. Below see a link to the error log http://pastebin.com/8FMjUFF5 Also, bellow you can see my code for that bit I am trying to do:

Public Sub Pizza()
    If Pizza_BaseTextBox.Text = 1 Then
        PizzaBase = 2.99
    ElseIf Pizza_BaseTextBox.Text = 2 Then
        PizzaBase = 4.99
    ElseIf Pizza_BaseTextBox.Text = 3 Then
        PizzaBase = 6.99
    ElseIf Pizza_BaseTextBox.Text = 4 Then
        PizzaBase = 8.99
    Else
        MsgBox("Please enter the correct number corresponding the pizza size you desire! See instructions")

    End If

    If CInt(Topping1TextBox.Text) > 0 And CInt(Topping1TextBox.Text) < 12 Then 'Error here
        Topping1 = 0.5
    Else
        Topping1 = 0
    End If
    If CInt(Topping2TextBox.Text) > 0 And CInt(Topping2TextBox.Text) < 12 Then
        Topping2 = 0.5
    Else
        Topping2 = 0
    End If
    If CInt(Topping3TextBox.Text) > 0 And CInt(Topping3TextBox.Text) < 12 Then
        Topping3 = 0.5
    Else
        Topping3 = 0
    End If
    If CInt(Topping4TextBox.Text) > 0 And CInt(Topping4TextBox.Text) < 12 Then
        Topping4 = 0.5
    Else
        Topping4 = 0
    End If
    If CInt(Topping5TextBox.Text) > 0 And CInt(Topping5TextBox.Text) < 12 Then
        Topping5 = 0.5
    Else
        Topping5 = 0
    End If

    PizzaTotal = PizzaBase + Topping1 + Topping2 + Topping3 + Topping4 + Topping5
End Sub

I would really appreciate it if I could receive some help about this.

Thanks a lot for the help and support, all comments are welcome.

Regards,

Jose rodriguez

3
  • Please post the exception message text along with your question (not the whole stack trace, only the message and line numbers). Also, check that you are not getting any empty strings when you try to convert them to integers. Commented Jun 2, 2014 at 11:34
  • 1
    option strict will prevent many run time problems like this by forcing you to write better code and perform explicit conversions Commented Jun 2, 2014 at 12:07
  • Pat, as you said my mistake was trying to convert empty strings into numbers. Now I am trying to assign all of my boxes the value of 1 on form load, but it only picks up at my first textbox but not on the others..any ideaS? Thanks Commented Jun 2, 2014 at 12:47

2 Answers 2

2
 CInt(Topping1TextBox.Text) > 0 

If the TextBox is empty CInt will throw an exception. Since it is user input, there is always the very real possibility that it is blank or contains "ziggy" instead of numerals.

Since it is user input, we can test and convert it at once:

Dim tmp As Integer
If Integer.TryParse(Topping1TextBox.Text, tmp) = False Then
   ' display error message that the value is invalid
Else
    If tmp > 0 And tmp < 12 Then
        Topping1 = 0.5
    Else
        Topping1 = 0
    End If
End If

' repeat pattern for the others

Integer.TryParse will return a Boolean indicating whether the string/text was able to be parsed into the variable passed. If True, then the variable will hold the value. All the data types have this function (Decimal.TryParse etc) and there are other similar functions such as Integer.Parse and Convert.ToInt32 for different situations.

The current program logic also does not account for a "-1" in the textbox. Usually, for something like this, a NumericUpDown is a better choice as it will force the input to be in a given range and numeric.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for this! It helps me so much more to understand coding and get done with this course! This fixed it for me, so thanks a lot :)
you're welcome - OPTION STRICT will help prevent this type of implicit conversion
0

You can check each textbox to see if its contents are numeric. If they are not numeric, then you ignore them or assign a default value.

For example:

Ignoring non-numeric:

If IsNumeric(Topping1TextBox.Text) Then
    If CInt(Topping1TextBox.Text) > 0 And CInt(Topping1TextBox.Text) < 12 Then 
        Topping1 = 0.5
    Else
        Topping1 = 0
    End If
End If

Default value for non-numeric:

If IsNumeric(Topping1TextBox.Text) AndAlso CInt(Topping1TextBox.Text) > 0 AndAlso CInt(Topping1TextBox.Text) < 12 Then
    Topping1 = 0.5
Else
    Topping1 = 0
End If

1 Comment

Thanks for the help, but the other guy got it first :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.