Question Can you check my code?

Pillaw

New member
Joined
Mar 24, 2013
Messages
4
Programming Experience
1-3
Hey, so basically, I have to do some exercises out of the Visual Basic 2008 book, and they are quiet simple, but I always make my code more complicated than needed. Basically, the program has to turn Mass into Weight by multiplying the users input for Mass by 9.8.

VB.NET:
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click        Dim Mass As Integer
        Dim Weight As Integer
        Mass = CInt(txtMass.Text)


        If Integer.TryParse(txtMass.Text, Mass) AndAlso Integer.TryParse(txtWeight.Text, Weight) = True Then
            txtWeight.Text = txtMass.Text * 9.8
            lblMessage.Text = "Successful Sequence!"
        Else
            MessageBox.Show("You must use a valid number", "Important Message", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

That is my code as of right now.. I need the program to display an error message when the user types in a letter, and if they type in an integer then the input for Mass is multiplied by 9.8 and displayed in the txtWeight. I think I need to use a CInt somewhere in here...
 
No you don't need to use CInt anywhere. Firstly, are you sure that the user can only enter integers and not decimal values?

Assuming that that is the case, Integer.TryParse is there to validate the input AND convert it. So, there's only one input so there's only one call to Integer.TryParse. You pass the input String and the output Integer to that call. If it returns False then the input was invalid. If it returns True then the input was valid and you can safely use the output. You can then multiply that output by your gravitational constant. If that's 9.8 though, it should be obvious that the result will not be an Integer. Once you've got that result, convert it to a String by calling its ToString method and display that.
 
No you don't need to use CInt anywhere. Firstly, are you sure that the user can only enter integers and not decimal values?

Assuming that that is the case, Integer.TryParse is there to validate the input AND convert it. So, there's only one input so there's only one call to Integer.TryParse. You pass the input String and the output Integer to that call. If it returns False then the input was invalid. If it returns True then the input was valid and you can safely use the output. You can then multiply that output by your gravitational constant. If that's 9.8 though, it should be obvious that the result will not be an Integer. Once you've got that result, convert it to a String by calling its ToString method and display that.

VB.NET:
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click        
        Dim sngMass As Integer
        Dim sngWeight As Integer
        Dim Message As String = lblMessage.Text 'Ignore This


        If Integer.TryParse(txtMass.Text, sngMass) And Integer.TryParse(txtWeight.Text, sngWeight) Then
            sngWeight = sngMass * 9.8
            Message = "Successful Sequence!"
            txtWeight.Text = sngWeight
        Else
            MessageBox.Show("You must use a valid number", "Important Message", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
This code continues to give me the MessageBox whenever I try to use letters or numbers of any value. Could you change around my code so I can see how it would work? Thank you
 
I specifically stated:
there's only one input so there's only one call to Integer.TryParse
Count the number of calls to Integer.TryParse in your code and tell me if there's only one. I will say again, Integer.TryParse is for validating and converting the input. If you don't know what's input and what isn't then you need to stop and have a good hard think about what you're doing. It doesn't take any programming experience to know the difference between input and output.
 
Alright, so I got that Mass & Weight calculator to work perfectly. I ended up only using one Integer.TryParse, but on another program that works flawlessly I used four. Now, I am having a little bit of trouble on why this new program is not working. If you have the book "Starting Out With: Visual Basic 2008" then you can find the program on page 271.

I am trying to use multiple Integer.TryParse, and it clearly states on page 243 that you can. Check out my code.. Tell me whats wrong please! Every time I enter a value like 100 for distance and 1.5 for weight I get the ending error message that I have set in a messagebox.

VB.NET:
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click        
        Dim intDistance As Integer
        Dim intWeight As Integer
        Dim intShippingCost As Integer
        Dim intShippingRate As Integer


        If Integer.TryParse(txtDistance.Text, intDistance) AndAlso Integer.TryParse(txtWeight.Text, intWeight) Then
            If (intDistance > 3000) Or (intDistance < 10) Or (intWeight > 20) Or (intWeight < 0) Then
                MessageBox.Show("This is a test error message")
            Else
                Select Case intWeight
                    Case Is < 0
                        MessageBox.Show("You must choose a higher weight!")
                    Case 0 To 2
                        intShippingRate = 0.01
                    Case 2.1 To 6
                        intShippingRate = 0.015
                    Case 6.1 To 10
                        intShippingRate = 0.02
                    Case 10.1 To 20
                        intShippingRate = 0.025
                    Case Is > 20
                        MessageBox.Show("You must choose a lower weight!")
                End Select
                intShippingCost = intShippingRate * intDistance
                lblShippingCost.Text = intShippingCost
            End If
        Else
            MessageBox.Show("You must use a number and not letters! Please check your input.", "Important Message", _
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub
 
Alright, so I was a little dumb and was using a decimal with an assigned integer value to the variables. I changed them to single, and might change them to decimal. Now my program is that the Select Case thingy isn't working how I want it to work.. Hopefully I can figure it out.
 
How many times can I say that you only call TryParse once? As I have said multiple times, TryParse is for validating and converting input. Is the user entering anything into 'txtWeight'? According to you, that's where you are outputting the result. It IS NOT input so DO NOT treat it as input.
 
Back
Top