|
-
Feb 8th, 2012, 07:59 PM
#1
Thread Starter
New Member
Test Score Calc
I'm not sure where I'm going wrong with this. it's supposed to let the user add scores to the list, then calculate the total, average, and count. However, it doesn't let me add more values to the list. Any help whatsoever would be awesome!
Keep in mind, I am new to VB and programming languages, and was just playing around with code hoping for any sort of positive result, and therefore some of the code may not be necessary, or make sense.
Here's what I have now:
Private Sub addbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbutton.Click
Dim scoreList As New List(Of Integer)
Dim score As Integer
If TextBox1.Text >= 0 And TextBox1.Text <= 100 Then
scoreList.Add(CInt(TextBox1.Text))
Dim num As Integer
For Each num In scoreList
Console.WriteLine(num)
Next
Dim i As Integer
For i = 0 To scoreList.Count - 1
Console.WriteLine(scoreList.Item(i))
Next i
score = TextBox1.Text
Dim scoreTotal As Integer = scoreTotal + score
TextBox2.Text = scoreTotal
Dim scoreCount As Integer = scoreCount + 1
TextBox3.Text = scoreCount
Dim scoreAverage As Decimal = Math.Round(scoreTotal / scoreCount, 2)
TextBox4.Text = scoreAverage
End If
End Sub
-
Feb 8th, 2012, 08:12 PM
#2
Re: Test Score Calc
Please use the Code or VBCode button provided to wrap your code snippets in formatting tags. They are too difficult to read otherwise.
Code:
Private Sub addbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbutton.Click
Dim scoreList As New List(Of Integer)
Dim score As Integer
If TextBox1.Text >= 0 And TextBox1.Text <= 100 Then
scoreList.Add(CInt(TextBox1.Text))
Dim num As Integer
For Each num In scoreList
Console.WriteLine(num)
Next
Dim i As Integer
For i = 0 To scoreList.Count - 1
Console.WriteLine(scoreList.Item(i))
Next i
score = TextBox1.Text
Dim scoreTotal As Integer = scoreTotal + score
TextBox2.Text = scoreTotal
Dim scoreCount As Integer = scoreCount + 1
TextBox3.Text = scoreCount
Dim scoreAverage As Decimal = Math.Round(scoreTotal / scoreCount, 2)
TextBox4.Text = scoreAverage
End If
End Sub
The issue is that you are creating a new List each time you click the Button. You use the List and discard it, then do the same next time. If you want to add multiple items to the same List then that's what you need to do, i.e. use the same List. That means creating one List to start with, before you click the Button at all, i.e OUTSIDE the Click event handler. You then add all the items to that one List.
-
Feb 8th, 2012, 09:16 PM
#3
Thread Starter
New Member
Re: Test Score Calc
Thanks for the help. It works perfectly after some more alterations to the code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|