Help!!!

I have VB homework due in 45 minutes and i cant get past an error message that I am recieving. I am supposed to be making a calorie counter and I cant get it to do any calculations. I was getting an error message for the parsing. It said "Input string was not in a correct format."

Public Class Form1

Private Sub exitbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitbutton.Click
'close the program.
Me.Close()

End Sub


Private Sub clearbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearbutton.Click
'clear all amounts on the form.
With Me
.carbsbox.Clear()
.proteinbox.Clear()
.calbox.Clear()
.Accumbox.Clear()
.itemsbox.Clear()
With .fatbox
.Clear()
.Focus()
End With
End With
End Sub





Private Sub calculatebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculatebutton.Click
'calculates the total of calories and the accumulated and item numbers.
Dim fatcalInteger As Integer
Dim carbscalInteger As Integer
Dim protiencalInteger, totalcalInteger, AccumcalInteger, NumberInteger As Integer

'convert input values to numeric variables.

fatcalInteger = Integer.Parse(Me.calbox.Text)
carbscalInteger = Integer.Parse(Me.carbsbox.Text)
protiencalInteger = Integer.Parse(Me.proteinbox.Text)

With Me

'calculate values.
totalcalInteger = (fatcalInteger * 8) + (carbscalInteger * 4) + (protiencalInteger * 4)

'count number inputed.
NumberInteger += 1



End With
End Sub
End Class



Answer this question

Help!!!

  • Andzej

    First of all : it would be better if you chose a subject line that describes your problem. Every person who posts here could put 'help' as a subject line, looking for help is why people post.

    Secondly, have you tried adding breakpoints and stepping through your code Did the IDE not tell you what line the error was on I can hazard a guess, but that's about all. For starters, you have this code :

    fatcalInteger = Integer.Parse(Me.calbox.Text)
    carbscalInteger = Integer.Parse(Me.carbsbox.Text)
    protiencalInteger = Integer.Parse(Me.proteinbox.Text)

    and I don't see any code that checks first if the text is able to be parsed as an integer. I know that there is a TryParse method that tells you if a value can be parsed, so I expect that the Parse method would be throwing an exception if an invalid value was passed through.

    It's generally bad form to create variables without default values, and also to declare them all at the top of a function. I can't imagine how this has come into the VB world, I know it comes into C++ via C, where it was required.



  • Amolw

    A better method is the TryParse, in this case: For example:

    Dim i As Integer
    Dim bReturn As
    Boolean
    bReturn = Integer
    .TryParse(TextBox1.Text, i)
    If bReturn = True
    Then
        MessageBox.Show(String.Format("You entered {0:0}"
    , i))
    Else
        MessageBox.Show(String.Format("Please enter an integer number between {0:0} and {1:0}", Integer.MinValue, Integer
    .MaxValue))
    End If

    No errors are thrown regardless as to the contents of the text box, Whereas both CInt and Parse will - as cgraus indicated.

    (edit: Parse appears to be fractonally faster than CInt, but not so you'd notice).



  • ivernot

    try

    'convert input values to numeric variables.

    fatcalInteger = CInt(Me.calbox.Text)
    carbscalInteger = CInt(Me.carbsbox.Text)
    protiencalInteger = CInt(Me.proteinbox.Text)


  • Medes_

    Then I don't understand why you suggested one over the other How is Integer.Parse his problem



  • sebatwerk

    Does CInt magically return 0 for a non numeric string



  • TheOneFD

    that's what I'm saying. And you're right both CInt() and Integer.Parse() work.

  • Oguz

    I did

    i = CInt("70")

    i = Integer.Parse("80")

    these both work

    i = CInt("string")

    i = Integer.Parse("test")

    these both throw exceptions. So what does CInt do that Integer.Parse does not

    It would still seem that he needs to validate his strings, either way.



  • Phil1234567

    He actually declared his variables as integers. Of course, you'll get an error if you try to convert a string i.e words, letters, y'know what I mean, into Integer. But, looking at his code I can clearly see that Integer.Parse() is the culprit. A very simple process that needs a very simple code indeed. Try duplicating the code and run it.

  • Help!!!