Click here to Skip to main content
15,848,157 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this question is about the voting procedure, the place im having problem is once the nomination of candidates is complete, i have to save the candidates in a array to continue the voting process, which is exactly what im having difficulty with.

What I have tried:

Private Sub btnnominate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnominate.Click
lstcandidate.Items.Add(txtname.Text)
End Sub


Private Sub btnconfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconfirm.Click
Dim candidates() As String
Dim i As Integer
ReDim candidates(lstcandidate.Items.Count)
For i =0 to lstcandidate.items.count
lstcandidate.SelectedIndex=i-1

candidates(i) = lstcandidate.Items(i)
im getting an error message in this, can someone kindly help me
Posted
Updated 5-Aug-16 21:12pm
Comments
Michael_Davies 6-Aug-16 1:45am    
Without information we cannot help, we cannot see your screen, what is the error message and which line?

For i =0 to lstcandidate.items.count

Should be:

For i =0 to lstcandidate.items.count - 1

Why set the selectedindex, no need.

1 solution

You for Loop goes out of the index. Do it like this and it should work:

VB.NET
Private Sub btnconfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconfirm.Click
  Dim candidates() As String
  ReDim candidates(lstcandidate.Items.Count)
  lstcandidate.Items.CopyTo(candidates, 0)
  //...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900