[RESOLVED] help with csv files and arrays
Hi all, so im new to VB.net, What I need to do is open a csv file which contains 4 fields and many rows, then I want to do different operations determined on the value in the fields.
for example :
My CSV will be in the following format.
ID,Name,Number,Status
Heres what I have so far
Code:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
Dim filestream As StreamReader
filestream = File.OpenText(OpenFileDialog1.FileName.ToString())
Dim readcontents As String
Dim counter As Integer
Dim Fails As Integer
Dim Delivered As Integer
Do Until filestream.EndOfStream
readcontents = filestream.ReadLine()
Dim textdelimiter As String
textdelimiter = ","
Dim splitout = Split(readcontents, textdelimiter)
counter = counter + 1
'Check the values of each field
Loop
filestream.Close()
strm.Close()
MessageBox.Show("counter:" & counter & "Records in file")
MessageBox.Show("Operation Complete")
End If
End Sub
My issue is that whilst in the loop I want to extract the data from the array and assign it to a variable but I don't know how to do this without receiving an out of bound exception ( I can do this simple task in perl and php but as I am new to VB.net I haven't the slightest idea how.
Could somebody point me in the right direction, I would appreciate it alot.
EDIT: - I want to perform an IF statement on each field of the csv.. e.g :
if status = 'good' then
do something
Else
do something else
Re: help with csv files and arrays
In place of this line:
Code:
'Check the values of each field
you could write something like this:
vb.net Code:
if splitout(0) = "good" then '~~~ Checks first part. ie., at index 0 of the array
'do something
else
'do something for false
end if
:wave:
Re: help with csv files and arrays
thanks for your reply!
apologies, I forgot to mention that this is what I was already attempting previously but I kept receiving an indexoutofbounds exception.
Please see below the code I had with this test in.
Code:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
Dim filestream As StreamReader
filestream = File.OpenText(OpenFileDialog1.FileName.ToString())
Dim readcontents As String
Dim counter As Integer
Dim Fails As Integer
Dim Delivered As Integer
Do Until filestream.EndOfStream
readcontents = filestream.ReadLine()
Dim textdelimiter As String
textdelimiter = ","
Dim splitout = Split(readcontents, textdelimiter)
counter = counter + 1
If splitout(3) = "DELIVERED" Then
Delivered = Delivered + 1
ElseIf splitout(3) = "FAILED" Then
Fails = Fails + 1
End If
'Check the values of each field
Loop
filestream.Close()
strm.Close()
MessageBox.Show("counter:" & counter & "Records in file with " & Fails & " Failures and " & Delivered & " Delivered.")
MessageBox.Show("Operation Complete")
End If
End Sub
Regards
minimatrix
Re: help with csv files and arrays
Here's how I tested it:
vb.net Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'~~~ A sample line from file
Dim tempFileContent As String = "123,Akhilesh,1,good"
'~~~ Splitting the string
Dim strArr() As String = tempFileContent.Split(","c)
'~~~ Checking it
If strArr(3).Trim.ToLower = "good" Then
MessageBox.Show("Wow ! You are in good condition..")
Else
MessageBox.Show("Hey... you are in bad condition")
End If
End Sub
End Class
:wave:
Re: [RESOLVED] help with csv files and arrays
Thanks for your help .. I realised that it was my input file that was incorrect and not the code .... my bad.
I really appreciate your help though.
This thread has been marked as resolved.