New User with VB.NET needs help...

TonLocDotNet

Newcomer
Joined
Oct 5, 2003
Messages
10
Location
Lakewood, CO
Im useing dbl in Dim,,,, do i realy have to ,,,,,,,, having problems with
calculation........... not to hard for most folks,,,,,,,,, but im just starting
to learn the basics...................thanks



Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

' Declaring Variable to store input and output from users
Dim dbltxtTest1 As Double
Dim dbltxtTest2 As Double
Dim dbltxtTest3 As Double
Dim dbltxtAverage As Double
Dim txtLetterGrade

' Calculate Average of three Test and Grade
dbltxtAverage = dbltxtTest1 + dbltxtTest2 + dbltxtTest3


txtAverage.Text = dbltxtAverage.ToString("###.##")


' Determins Grade per Average
Select Case dbltxtAverage
Case 90 To 100
txtLetterGrade = "A"
Case 79 To 89
txtLetterGrade = "B"
Case 69 To 78
txtLetterGrade = "C"
Case 68 To 59
txtLetterGrade = "D"
Case Is < 59
txtLetterGrade = "F"
End Select

End Sub
 
I have declared my DIM,,,,,, using the Double,,, dbl and am also using it in my calculation,,,,,,,, im just unclear if i have to use it..... but i cant get the average to calculate and show up as output in my form.............. not sure why,,,,, i still dont understand the basics............
 
You have to add the three numbers and divide by three, not just add the three numbers.
Visual Basic:
dbltxtAverage = (dbltxtTest1 + dbltxtTest2 + dbltxtTest3) / 3
Also your variable txtLetterGrade is 1) not being declared as anything (you should use As String for textual variables), 2) not being used once you set it to the proper letter grade.
 
here is the latest and greatest........ still not showing up in Average...... i was woking on the calculation,,,, as far as deviding,,,,,, but for some reason the average is not passing in the variable.................


Dim dbltxtTest1 As Double
Dim dbltxtTest2 As Double
Dim dbltxtTest3 As Double
Dim dbltxtAverage As Double
Dim txtLetterGrade As String




' Calculate Average of three Test and Grade
dbltxtAverage = (dbltxtTest1 + dbltxtTest2 + dbltxtTest3) / 3

' txtTest1.Text = dbltxtTest1.ToString("###.##")
' txtTest2.Text = dbltxtTest2.ToString("###.##")
' txtTest3.Text = dbltxtTest3.ToString("###.##")

******NOT SURE IF I NEED THESE TO CONVERT TO STRING****


txtAverage.Text = dbltxtAverage.ToString("###.##")





' Determins Grade per Average
Select Case dbltxtAverage
Case 90 To 100
txtLetterGrade = "A"
Case 79 To 89
txtLetterGrade = "B"
Case 69 To 78
txtLetterGrade = "C"
Case 68 To 59
txtLetterGrade = "D"
Case Is < 59
txtLetterGrade = "F"
End Select


End Sub
 
WOW,,,,,,,,,,,,, I thought this was pretty basic stuff,,,,,,

Common guys & gals...... you remember what it was like when
you started out,,,,,,,,,,,,,, Maybe you can help me,,,,,, and one day ILL return the favor,,,,,,,,,,,,,,,,,, :}
 
OK, I'm not sure if that's your whole code, but if it is, I don't know what you expect it to output. You are declaring the variables dbltxtTest1 etc and using them without ever giving them a value.

However, if you want to use controls on the form (txtTest1, etc), you need to use Double.Parse() for that. After:
Visual Basic:
Dim dbltxtTest1 As Double
Dim dbltxtTest2 As Double
Dim dbltxtTest3 As Double
Dim dbltxtAverage As Double
Dim txtLetterGrade As String
put this:
Visual Basic:
dbltxtTest1 = Double.Parse(txtTest1.Text)
dbltxtTest2 = Double.Parse(txtTest2.Text)
dbltxtTest3 = Double.Parse(txtTest3.Text)
 
One thing I've just noticed, is txtLetterGrade a textbox? It so you'll need to update it with the text property...


Code:
 Determins Grade per Average
Select Case dbltxtAverage
Case 90 To 100
txtLetterGrade.Text  = "A"
Case 79 To 89
txtLetterGrade.Text   = "B"
Case 69 To 78
txtLetterGrade.Text   = "C"
Case 68 To 59
txtLetterGrade.Text   = "D"
Case Is < 59
txtLetterGrade.Text   = "F"
End Select
 
Thanks for the help,,,, ALL..... Im getting closer..... my calculation for the Average is not working.....



' Calculate Average of three Test and Grade
dbltxtAverage = (dbltxtTest1 + dbltxtTest2 + dbltxtTest3) / 3

txtTest1.Text = dbltxtTest1.ToString("###.##")
txtTest2.Text = dbltxtTest2.ToString("###.##")
txtTest3.Text = dbltxtTest3.ToString("###.##")

******NOT SURE IF I NEED THESE TO CONVERT TO STRING****
****** DO I REALY NEED TO CONVERT THE TEST#***********
*** ***
****THESE txtTest# <<< ARE INPUTED BY THE USER THEN ****
**** THE CALCULATION TAKES PLACE............ ****


txtAverage.Text = dbltxtAverage.ToString("###.##")
 
OK, these are your problems.

1. You're not assigning any values to the dbltxtTest1, dbltxtTest2, or dbltxtTest3 variables. They are all set to 0 by default. When you find the average of 0, its 0.

Solution: Set the variables. Try adding
dbltxtTest1 = 1
dbltxtTest2 = 2
dbltxtTest1 = 3

In your code somewhere, or add 3 textboxes, and assign the variables from these (eg dbltxtTest1 = cdbl(txtTest1.text))

2. When you display the results using the "###.##" format, it outputs "" because the values are all 0.

Solution:
If you want to display 0, try using "##0.##", this will always display the first digit, and the rest of the digits (#) are optional.


Hope this helps...
 
Please Remember,,,, Im new at this...... Im appreciate the help..... but most of it is GREEK to me......

Im sorry I didnt explain.... The User inputs the values for
txtTest 1-3........ on the form..... after values are inputed ,,,
then the user clicks the Calculate button........... that when
Average kicks in............ So I dont want Constants in my Code..

Can you show me by code what is wrong with the Calculation...
 
I copied your code into a form I created, added 4 textboxes (test 1, 2, & 3, and a Average box, and a button.

Code:
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim dbltxtTest1 As Double
        Dim dbltxtTest2 As Double
        Dim dbltxtTest3 As Double
        Dim dbltxtAverage As Double
        Dim txtLetterGrade As String

        'added a bit of validation here
        If Len(txtTest1.Text) = 0 Or Not IsNumeric(txtTest1.Text) Then
            dbltxtTest1 = 0
        Else
            dbltxtTest1 = CDbl(txtTest1.Text)
        End If
        If Len(txtTest2.Text) = 0 Or Not IsNumeric(txtTest2.Text) Then
            dbltxtTest2 = 0
        Else
            dbltxtTest2 = CDbl(txtTest2.Text)
        End If
        If Len(txtTest3.Text) = 0 Or Not IsNumeric(txtTest3.Text) Then
            dbltxtTest3 = 0
        Else
            dbltxtTest3 = CDbl(txtTest3.Text)
        End If

        ' Calculate Average of three Test and Grade
        dbltxtAverage = (dbltxtTest1 + dbltxtTest2 + dbltxtTest3) / 3

        'format the test numbers
        txtTest1.Text = dbltxtTest1.ToString("##0.##")
        txtTest2.Text = dbltxtTest2.ToString("##0.##")
        txtTest3.Text = dbltxtTest3.ToString("##0.##")

        'Return the formatted average
        txtAverage.Text = dbltxtAverage.ToString("##0.##")
    End Sub
 
This is a bit over my head.......... sorry,,, I know your tring to help..... when you say validation,,,,,, you mean your just testing
the code........... ILL try it
 
Yeh, thats right, the validation helps stupid users :)

The len() function bit checks if anything is in the text box, and the isnumeric() function checks if there is a number in the textbox. You can leave all that off if you like, but it might cause errors. Experiment, leave it in, and then remove parts to see what happens.
 
Last edited:
Back
Top