Public Class FoodSurveryForm
' handles Food Survey Form's Load event
Private Sub FoodSurveryForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' String array for food names
Dim foods As String() = {"Cheese Pizza", "Hamburger", "Fish(Sticks)", "Mystery Meat"}
Array.Sort(foods) ' alphabetize food names
foodsComboBox.DataSource = foods ' display country names in ComboBox
foodsComboBox.SelectedIndex = 0 ' select first food in list
resultsListBox.Items.AddRange(foods) _
End Sub ' FoodSurveryForm_Load
Private votes(3, 1) As Integer
Private Sub voteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles voteButton.Click
Dim index As Integer = foodsComboBox.SelectedIndex
2:
3: ' Loop through the number of items
4: For counter As Integer = 0 To 3
5: ' If the counter is equal to the index you got from the combo
6: ' Check the radio buttons.
7: If counter = index Then
8: ' If statement that checks radio button
9: If likeRadioButton.Checked Then
10: ' Update column 0 by adding 1 for likes
11: votes(counter, 0) += 1
resultsListBox.Items.Add(ControlChars.Tab & ControlChars.Tab _
& ControlChars.Tab & index)
12: ElseIf dislikeRadioButton.Checked Then
13: ' Update column 1 by adding 1 for dislikes
14: votes(counter, 1) += 1
resultsListBox.Items.Add(ControlChars.Tab & ControlChars.Tab & index)
15: End If
16:
17: ' No matter which choice is made, one item to add to results
18: ' So no use duplicating this for one if or the other.
19:
20: End If
21: Next
End Sub
End Class ' FoodSurveryForm
counting votes using an array
Page 1 of 15 Replies - 237 Views - Last Post: 27 January 2012 - 01:16 PM
Topic Sponsor:
#1
counting votes using an array
Posted 27 January 2012 - 12:04 PM
Hi all. I am new to programing. I am trying to count the number of like and dislike votes for a "Food Survey" I have a (4,2) array the food items are 4 and the voting options are the 2. My counting is so off and it's not displaying right. What is supposed to happen is a menu item is selected from the combobox and user can select like or dislike then click the vote button. Ultimately the in the resultsListbox there should be the menu items and the total likes and dislikes. Any suggestions would be appreciated. Thanks.
Replies To: counting votes using an array
#2
Re: counting votes using an array
Posted 27 January 2012 - 12:40 PM
Cyndi, on 27 January 2012 - 01:04 PM, said:
Ultimately the in the resultsListbox there should be the menu items and the total likes and dislikes. Any suggestions would be appreciated. Thanks.
Suggestions for... What? You haven't told us of any problems you're having or errors you're trying to overcome. What exactly is your question?
Obviously this isn't a real-world application. So I assume this is for school homework.
What's with the screwy numbers and colons in the middle of the code block?
10:
11:
etc.
Is this code you copy/pasted from the internet and now doesn't work because of that?
#3
Re: counting votes using an array
Posted 27 January 2012 - 12:46 PM
This is indeed homework. There are no error messages. My loops is not working right. I am trying to get a continual count of the likes and dislikes for each food item. Right now the like isn't calculating anything. the Dislike is calculating (but not continually I hope that helps explain more. And when the last item in the comboBox is selected it automatically sets 3 votes. LOL. I supposed I should start with just getting the calculations correct then worry about the display.
#4
Re: counting votes using an array
Posted 27 January 2012 - 01:03 PM
Take a second look at this logic
Is this really going to get you the number of votes?
I thought you said you were using a 4,2 array: 4 items, 2 votes.
Unless VB is different than C# this would make a 3,1 array
0,1,2 x 0
Dim index As Integer = foodsComboBox.SelectedIndex ' do some stuff resultsListBox.Items.Add(ControlChars.Tab & ControlChars.Tab & index)
Is this really going to get you the number of votes?
I thought you said you were using a 4,2 array: 4 items, 2 votes.
Private votes(3, 1) As Integer
Unless VB is different than C# this would make a 3,1 array
0,1,2 x 0
#5
Re: counting votes using an array
Posted 27 January 2012 - 01:06 PM
tlhIn`toq, on 27 January 2012 - 01:03 PM, said:
Take a second look at this logic
Is this really going to get you the number of votes?
I thought you said you were using a 4,2 array: 4 items, 2 votes.
Unless VB is different than C# this would make a 3,1 array
0,1,2 x 0
Dim index As Integer = foodsComboBox.SelectedIndex ' do some stuff resultsListBox.Items.Add(ControlChars.Tab & ControlChars.Tab & index)
Is this really going to get you the number of votes?
I thought you said you were using a 4,2 array: 4 items, 2 votes.
Private votes(3, 1) As Integer
Unless VB is different than C# this would make a 3,1 array
0,1,2 x 0
#6
Re: counting votes using an array
Posted 27 January 2012 - 01:16 PM
I am thinking maybe I need to assign the array at that point or convert the selected item in the combo box to an integer.
I am heading home from work now, but will run through it in my head on the way home. Thanks.
I am heading home from work now, but will run through it in my head on the way home. Thanks.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|