Comparison operators in a collection class

INFLICT

New member
Joined
Jul 12, 2005
Messages
3
Programming Experience
Beginner
Greetings,
I have a collection class and a windows application that will allow a user to input test scores. When the user inputs a score he or she will then click on the add score button. The add Score button will add that particular score to a collection.
I have 2 issues that assistance would be appreciated. First issue is that there is also several buttons for this application including Maximum Score and Minimum Score button. When you hit the Maximum button code similar to the following executes:

VB.NET:
Dim intScore As Integer
Dim intScore1 As Integer = 0
For Each NewScore in ScoreCollection
If ScoreCollection.Count is = 1 Then
intScore = NewScore
Else
intScore1 = NewScore
If intScore1 > intScore
intScore = intScore1
End if
End if
MessageBox.Show(intScore, "Maximum Score")
This function works fine as the correct results are displayed however for the minimum button when I change the comparison operator to < then after the 2nd Score is added to the collection the minimum score shows 0 when I haven't input 0 as a score result. ????

Second issue is that I don't know how to reset the collection. I wanted to add a reset button with code similar to the following:

VB.NET:
btnReset_Click(.......................
nudInputScoreAmount.Value = 0
TextBox1.Text = ""
TextBox2.Text = ""
ect.
This resets the display in the application window, however the maximum score, average score, minimum score buttons all retain their value. How can I reset the collection using VB .NET code?

Any help would be appreciated. Thanks,
The next Bill Gates.
 
For the first issue you need to use a breakpoint to stop execution when you enter the Minimum calculation and step through the code one line at a time while using the Locals or Watch window to follow the values of each variable. This is the procedure you should always follow to try to determine what causes bugs like this. You'll soon see what the problem is.

For the second issue, I assume that you've done the right thing and derived your custom collection class from CollectionBase. If so, it has inherited a Clear method from that class, so you simply call Clear on your collection object.
 
Resolved

Thanks for the assistance I was able to fix the first issue after using breakpoints. I still didn't understand what the problem was because the assignment statement

intScore = gobjNewScore

for example, didn't work. The intScore value did not retain the value assigned to is (gobjNewScore) by the assignment statement. What I did to resolve it was declare the intScore variable 2 times once inside the loop and once outside the For Nex loop for this to work. If I removed one of the variable instantiation's then the minimum button would not work properly.
As for using the Clear() method to reset the collection that didn't work because the collection was declared within the windows application and not the class. The class is where the collectionBase was inherited, therefore the clear method was not inherited into the windows application. I'm sure I could of just passed the values from the class to the windows application but when I tried to do that I got an error message that the variables were not declared. However they were declared only in the class and not the windows application. If you bother looking at this thread again is it possible that you can show me how to correctly pass the values from the class to the windows application form?

Thanks for all the help.
 
Here's how I would implement your max and min score functions.
VB.NET:
	Public Function GetMaxScore() As Integer
		If Me.scoreCollection.Count = 0 Then
			Return 0
		End If

		Dim maxScore As Integer = Me.scoreCollection(0)

		For i As Integer = 1 To Me.scoreCollection.Count - 1 Step 1
			If Me.scoreCollection(i) > maxScore Then
				maxScore = Me.scoreCollection(i)
			End If
		Next i

		Return maxScore
	End Function

	Public Function GetMinScore() As Integer
		If Me.scoreCollection.Count = 0 Then
			Return 0
		End If

		Dim minScore As Integer = Me.scoreCollection(0)

		For i As Integer = 1 To Me.scoreCollection.Count - 1 Step 1
			If Me.scoreCollection(i) < minScore Then
				minScore = Me.scoreCollection(i)
			End If
		Next i

		Return minScore
	End Function
This assumes that you have an instance of your collection class declared the class level and called "scoreCollection". You could alter this code a little to put the calculations themselves inside the Buttons' event handlers and display the scores as needed.

As for clearing the collection, assuming that your form has an instance of the collection class named scoreCollection, you simply clear that collection like so:
VB.NET:
Me.scoreCollection.Clear()
 
Back
Top