Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way that I can simplify my code using a loop. in Visual Basic

I have a program that looks at votes entered for candidates and determines the winner by who has the lowest score. Because the user enters their preferences from 1 to 5 with 1 being the favorite and 5 being the least favorite. If there is a draw the candidate with the most 1's win, if this is still a draw then its the candidate with the most 2's. I have a program but want to know if I can simplify it before I continue to repeat the code to check who got the most 2's ect. This is what I have so far.

    If candidate1 < candidate2 And candidate1 < candidate3 And candidate1 < candidate4 And candidate1 < candidate5 Then
        MsgBox("The winner is" & LblCand1.Text)
    ElseIf candidate2 < candidate1 And candidate2 < candidate3 And candidate2 < candidate4 And candidate2 < candidate5 Then
        MsgBox("The winner is" & lblCand2.Text)
    ElseIf candidate3 < candidate2 And candidate3 < candidate1 And candidate3 < candidate4 And candidate3 < candidate5 Then
        MsgBox("The winner is" & lblCand3.Text)
    ElseIf candidate4 < candidate2 And candidate4 < candidate3 And candidate4 < candidate1 And candidate4 < candidate5 Then
        MsgBox("The winner is" & lblCand4.Text)
    ElseIf candidate5 < candidate2 And candidate5 < candidate3 And candidate5 < candidate4 And candidate5 < candidate1 Then
        MsgBox("The winner is" & lblCand5.Text)
    ElseIf C1V1s > C2V1s And C1V1s > C3V1s And C1V1s > C4V1s And C1V1s > C5V1s Then
        MsgBox("The winner is" & LblCand1.Text)
    ElseIf C2V1s > C1V1s And C2V1s > C3V1s And C2V1s > C4V1s And C2V1s > C5V1s Then
        MsgBox("The winner is" & lblCand2.Text)
    ElseIf C3V1s > C1V1s And C3V1s > C2V1s And C3V1s > C4V1s And C3V1s > C5V1s Then
        MsgBox("The winner is" & lblCand3.Text)
    ElseIf C4V1s > C1V1s And C4V1s > C2V1s And C4V1s > C3V1s And C4V1s > C5V1s Then
        MsgBox("The winner is" & lblCand4.Text)
    ElseIf C5V1s > C1V1s And C5V1s > C2V1s And C5V1s > C3V1s And C5V1s > C4V1s Then
        MsgBox("The winner is" & lblCand5.Text)
    End If
like image 254
Rachael Middleditch Avatar asked Dec 27 '25 18:12

Rachael Middleditch


1 Answers

I would recommend that you create a class Candidate which contains a score and a name for each candidate:

Class Candidate
    Implements IComparable(Of Candidate)

    Public Property Score As Integer
    Public Property Name As String

    Public Function CompareTo(other As Candidate) As Integer Implements IComparable(Of Candidate).CompareTo
        If (other.Score > Me.Score) Then
            Return -1
        ElseIf (other.Score = Me.Score) Then
            Return 0
        Else
            Return 1
        End If
    End Function
End Class

Then, if you have a collection of your candidate instances, you can do the following:

Dim myCandidates As New List(Of Candidate)
'fill your list with instances somehow...
MsgBox(String.Format("The winner is {0}", myCandidates.Min().Name))

Note that the Candidate class has to implement the IComparable(Of T) interface so the instances know how to compare themselves from the Min() method.

like image 189
rory.ap Avatar answered Dec 31 '25 00:12

rory.ap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!