Hi, im working on a Computing A Level project, it involves making a virtual Ballot Box.

One of the things the Ballot Box is required to do is to choose the winner by determining which candidate has received the highest number of votes.

Has anyone got ANY IDEA how i could do this? Im unbelievably stuck!?

Im writing it as a VB console application, here is how im storing the total votes for each candidate:

' Candidate Array
    Dim total_cand As Integer
    Dim Candidate(2, 10) As String

The 1st dimension of the array stores the Candidates name, and the 2nd stores the total votes they have received.

Any help would be greatly appreciated! :)

Option Explicit

Dim NumberOfCanidates As Integer
Dim CanidateNames() As String
Dim CanidateVotes() As Integer

Private Sub Command1_Click()
If Trim(Text1.Text) <> vbNullString Then
  NumberOfCanidates = NumberOfCanidates + 1
  ReDim Preserve CanidateNames(1 To NumberOfCanidates) As String
  ReDim Preserve CanidateVotes(1 To NumberOfCanidates) As Integer
  CanidateNames(NumberOfCanidates) = Text1.Text
  Combo1.AddItem Text1.Text
End If
End Sub

Private Sub Command2_Click()
Dim ForLoopCounter As Integer
For ForLoopCounter = 1 To NumberOfCanidates
  If CanidateNames(ForLoopCounter) = Combo1.Text Then
    CanidateVotes(ForLoopCounter) = CanidateVotes(ForLoopCounter) + 1
    Exit For
  End If
Next ForLoopCounter
End Sub

Command1 adds canidates to the array from values entered into text1.
command2 finds which canidate was selected from the combo box and increments his/hers vote by one.

Very simple example but should get you started.

Good Luck

This article has been dead for over six months. Start a new discussion instead.