need help in a program for school

cookiemonster

New member
Joined
Apr 29, 2009
Messages
3
Programming Experience
Beginner
I'm takeing a vb class in my college and need a a little help here is the my problem is that when i run the program if i put Calories as 200 and fat as any thing grater than 2

VB.NET:
If txtCalories.Text < txtFat.Text Then
                MessageBox.Show("Grams of fat cannot be greater than total calories please check your data and try again")

executes so if i put somehthing like calories = 200 fat = 8 i get he message i was sjut wondering what i did wrong

VB.NET:
    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

        '==================================================='
        ' Calculate the percentage useing the given amounts '
        '==================================================='

        Dim dblPercent_From_Fat As Double

        '==================================================='
        Try
            If txtCalories.Text < txtFat.Text Then
                MessageBox.Show("Grams of fat cannot be greater than total calories please check your data and try again")
                If CDbl(txtFat.Text) < 0 Then
                    MessageBox.Show("Grams of Fat Cannot be less than 0 please check your informationa and try again")
                End If
                If CDbl(txtCalories.Text) < 0 Then
                    MessageBox.Show("Calories Cannot be less than 0 please check your Information and try again")
                End If

            Else
                dblPercent_From_Fat = CDbl(txtFat.Text) * CDbl(9) / _
                CDbl(txtCalories.Text)
                lblPercent_of_Fat.Text = dblPercent_From_Fat.ToString("p")
            End If

        Catch ex As Exception
            MessageBox.Show("Must Be Calculated as Numbers", _
                            "Error")
        End Try

    End Sub
 
I'll second everything Robert put in his post.

This is no disrespect to you, but I really wish tutors would explain about Option Strict in the first lesson - it would save a lot of headaches for students :)
 
The reason for your problem is that you are doing a textual-based compare, rather than a numeric compare. "200" and "2000" are less than "8", but 8 is less than 200 and 2000.

To instantly solve it, change your code to :-

VB.NET:
CDbl (txtCalories.Text) < CDbl (txtFat.Text)

but consider using NumericUpDown controls as indicated earlier.
 
There's a "greater/lower then" operator defined for String. oO'
Well, it really works but it's nowhere documented...Reflector doesn't show any operators or a inheritance either...I wonder where that one is coming from...

Also consider using the Convert Class instead of the VB6 like CDbl/CInt conversions. A little test showed me that the Convert Class takes only half the time the C-Conversions need.

Bobby
 
Two other points for the Convert class :-

1. It will remind you what size of integer you are converting to - ie ToInt16, ToInt32 etc

2. Dont forget to Import the Convert class, so you dont have to type Convert.ToInt32 etc all the time (took me a while to realise this :eek: )
 
Using the inline Type Conversion Functions is preferred.

That said, when handling input from user you can't be sure the input is valid to convert to any given type, so you can use things like Integer.TryParse or better an input control that is designed for numeric input, such as the NumericUpDown control.
 
i need help again

ok it works except for second place i'm not sure what i did wrong from what i can see it should store the time value that got first place in int1st and store the one that got third place in int3rd then it should compare the two and detiremin second place by this logic if txtRunner1Time > int1st but < int3rd time then
lbl2nd.Text = txtRunner1.Text
here i put it on paste bin here
http://pastebin.com/m3af2f8e8l
and i'll include the code in my post as well maybe it is something simple that i over looked its for school again just some hints might be helpful

VB.NET:
Option Strict On
Option Explicit On

Public Class frmRace


    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

        '======================================================='
        ' Calculate who the person who came in 1st, 2nd and 3rd '
        '======================================================='

        Dim int1st As Integer
        Dim int3rd As Integer

        '======================================================='

        Try

            If CType(txtRunner1Time.Text, Integer) < CType(txtRunner2Time.Text, Integer) And CType(txtRunner1Time.Text, Integer) < CType(txtRunner3Time.Text, Integer) Then
                int1st = CType(txtRunner1Time.Text, Integer)
                lbl1st.Text = txtRunner1.Text
            End If

            If CType(txtRunner2Time.Text, Integer) < CType(txtRunner1Time.Text, Integer) And CType(txtRunner2Time.Text, Integer) < CType(txtRunner3Time.Text, Integer) Then
                int1st = CType(txtRunner2Time.Text, Integer)
                lbl1st.Text = txtRunner2.Text
            End If

            If CType(txtRunner3Time.Text, Integer) < CType(txtRunner1Time.Text, Integer) And CType(txtRunner3Time.Text, Integer) < CType(txtRunner2Time.Text, Integer) Then
                int1st = CType(txtRunner3Time.Text, Integer)
                lbl1st.Text = txtRunner3.Text
            End If

            '===================================================================================================================================='

            If CType(txtRunner1Time.Text, Integer) > CType(int1st, Integer) And CType(txtRunner1Time.Text, Integer) < CType(int3rd, Integer) Then
                lbl2nd.Text = txtRunner1.Text
            End If

            If CType(txtRunner2Time.Text, Integer) > CType(int1st, Integer) And CType(txtRunner2Time.Text, Integer) < CType(int3rd, Integer) Then
                lbl2nd.Text = txtRunner2.Text
            End If

            If CType(txtRunner3Time.Text, Integer) > CType(int1st, Integer) And CType(txtRunner3Time.Text, Integer) < CType(int3rd, Integer) Then
                lbl2nd.Text = txtRunner3.Text
            End If


            '=============================================================================================================================================================='

            If CType(txtRunner1Time.Text, Integer) > CType(txtRunner2Time.Text, Integer) And CType(txtRunner1Time.Text, Integer) > CType(txtRunner3Time.Text, Integer) Then
                int3rd = CType(txtRunner1Time.Text, Integer)
                lbl3rd.Text = txtRunner1.Text
            End If

            If CType(txtRunner2Time.Text, Integer) > CType(txtRunner1Time.Text, Integer) And CType(txtRunner2Time.Text, Integer) > CType(txtRunner3Time.Text, Integer) Then
                int3rd = CType(txtRunner2Time.Text, Integer)
                lbl3rd.Text = txtRunner2.Text
            End If

            If CType(txtRunner3Time.Text, Integer) > CType(txtRunner1Time.Text, Integer) And CType(txtRunner3Time.Text, Integer) > CType(txtRunner2Time.Text, Integer) Then
                int3rd = CType(txtRunner3Time.Text, Integer)
                lbl3rd.Text = txtRunner3.Text
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        '=================='
        ' Clear all Feilds '
        '=================='

        txtRunner1.Clear()
        txtRunner2.Clear()
        txtRunner3.Clear()
        txtRunner1Time.Clear()
        txtRunner2Time.Clear()
        txtRunner3Time.Clear()
        lbl1st.Text = String.Empty
        lbl2nd.Text = String.Empty
        lbl3rd.Text = String.Empty
        txtRunner1.Focus()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        '======================'
        'Terminate the program '
        '======================'

        Me.Close()

    End Sub
End Class
 
Back
Top