Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,137 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,230 people online right now. Registration is fast and FREE... Join Now!




index of method, and select case

 
Reply to this topicStart new topic

index of method, and select case

Zigried
21 Jul, 2008 - 02:52 PM
Post #1

New D.I.C Head
*

Joined: 20 Jul, 2008
Posts: 2

Any help that anyone could offer would be greatly appreciated. As you can see in the following code I have not written any code using the select case function. I understand how to write the select case statement, but how do I tally up the "0,1,2" so that I get a total for the amount of votes.


Warren High Pseudocode
Save the selectedIndex of each vote and vbnewline, so the next vote will be written on a new line.
Display Code:
if fileexists
var = readalltext
newlineposition = using theindexof method starting from present position variable find the position of vbnewline. newlineposition = var.indexof(vbnewline,present position variable)
do until newlineposition = -1 'did not find newline, therefore end of file.
using the substring method, get each vote starting at the presentposition, and the number of characters will be the newlineposition minus the presentposition. The syntax for the substring is var.substring(starting position, number of characters)
using the Select case add the votes which will be a "0","1", or "2".
add 2 to the newlineposition to get the new present position of the vote
using the indexof method from above get the next vote
end loop
display results





vb

Option Explicit On
Option Strict On




Public Class MainForm
Private candidates() As String = {"Mark Stone", "Shelma Patel", "Sam Perez"}
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For subscript As Integer = 0 To 2
Me.candidateListBox.Items.Add(candidates(subscript).ToString)

Next
End Sub

Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click

If MessageBox.Show("Are you sure you want to exit?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()

End If


End Sub

Private Sub saveVoteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles saveVoteButton.Click
Dim file As String = "VoteTally"
Dim votes As String
votes = Me.candidateListBox.SelectedIndex.ToString

My.Computer.FileSystem.WriteAllText(file, votes & vbNewLine, True)



End Sub

Private Sub displayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles displayButton.Click
Dim str As String
Dim endofLine As Integer
Dim newlinePosition As Integer
Dim tally As String
Dim totalVotes As Decimal


If My.Computer.FileSystem.FileExists("VoteTally") Then
str = My.Computer.FileSystem.ReadAllText("VoteTally")
endofLine = str.IndexOf(vbNewLine, newlinePosition)

Do Until endofLine = -1


tally = str.Substring(newlinePosition, endofLine - newlinePosition)
totalVotes += Convert.ToDecimal(tally)

Me.candidateListBox.Items.Add(tally)

newlinePosition = str.IndexOf(vbNewLine, newlinePosition)
Loop

Me.candidateListBox.Items.Add(totalVotes)

End If


End Sub
End Class


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Index Of Method, And Select Case
21 Jul, 2008 - 02:58 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
This is VB.NET code so I'm moving this to that forum so it can get the attention of the proper experts smile.gif
User is offlineProfile CardPM
+Quote Post

Damage
RE: Index Of Method, And Select Case
21 Jul, 2008 - 07:03 PM
Post #3

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
sorry i'm not sure i follow. do you mean something like this?

ahh reading from file, my bad

This post has been edited by Damage: 22 Jul, 2008 - 02:00 AM
User is online!Profile CardPM
+Quote Post

kikz
RE: Index Of Method, And Select Case
22 Jul, 2008 - 01:14 AM
Post #4

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 32



Thanked: 1 times
My Contributions
Rather than searching for vbNewLine, read the file line by line by using ReadLine();

Something like :
CODE

Dim votes() As Integer = {0, 0, 0}

        Using sr As System.IO.StreamReader = New System.IO.StreamReader("myfile.txt")
            While sr.EndOfStream = False
                Dim line As String = sr.ReadLine().Trim()

                While line.Length > 0

                    ' Get the vote at the front of the string
                    Dim vote As Integer = CType(line.Substring(0, 1), Integer)

                    votes(vote) += 1

                    ' drop the left most character
                    If line.Length > 1 Then
                        line = line.Substring(1)
                    Else
                        line = ""
                    End If
                End While
            End While
        End Using


The main "trick" is to create an array with all possible voting options 1, 2, 3
User is offlineProfile CardPM
+Quote Post

Zigried
RE: Index Of Method, And Select Case
22 Jul, 2008 - 07:26 PM
Post #5

New D.I.C Head
*

Joined: 20 Jul, 2008
Posts: 2

Thanks for the help. That code does seem to work better, but how do I read the file and get the total for each candidate. Any help you could provide would be great. Even a step in the right direction.
User is offlineProfile CardPM
+Quote Post

kikz
RE: Index Of Method, And Select Case
22 Jul, 2008 - 11:35 PM
Post #6

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 32



Thanked: 1 times
My Contributions
The total of each candidate can be retrieved by accessing the appropriate element of the array.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:59AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month