|
-
Apr 1st, 2004, 03:47 PM
#1
Thread Starter
New Member
Syntax HELP: Array/CSV Split
I basically need to read a CSV text file with multiple entries that looks like such:
Smith,John,(111)-111-1111
Michaels,Gary,(222)-222-2222
Jones,Bill,(333)-333-3333
I am then trying to store the names in a memberData array and the phone #'s in a phoneData array respectively. Below is the code in which I have so far:
Dim fileName, line, Message As String
Dim temp As String()
Dim memberData() As String = {}
Dim phoneData() As String = {}
Dim i, counter, upperBound As Integer
Dim sr As IO.StreamReader
'-----------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fileName = "Members.txt"
counter = 0
upperBound = 0
If IO.File.Exists(fileName) Then
sr = IO.File.OpenText(fileName)
Else
Message = "Either no file has yet been created or the file "
Message &= fileName & " is not where expected."
MsgBox(Message, , "File Not Found")
End If
Code:
For i = 0 To 4
Do While (sr.Peek() <> -1)
line = sr.ReadLine
temp = line.Split(","c)
ReDim Preserve memberData(i)
memberData(i) = temp(0) & "," & temp(1)
Loop
Next
lstNames.Items.Add(memberData(0))
sr.Close()
End Sub
The problem I am running into is that the temp array locations 0 and 1 keep getting overwritten and the last entry in the text file is just being stored instead of all the entries. I have been going at this for a while and can't seem to get the right syntax. Please help.
-
Apr 1st, 2004, 10:26 PM
#2
VB Code:
Private Structure Member
Dim FirstName As String
Dim LastName As String
Dim PhoneNumber As String
End Structure
Private Function LoadMembers(ByVal strFile As String) As Member()
Const ErrMsg As String = "Either no file has yet been created or the file is not where it was expected."
If IO.File.Exists(strFile) = False Then
Throw New IO.FileNotFoundException(ErrMsg, strFile)
Exit Function
End If
Dim sr As New IO.StreamReader(strFile)
Dim strData() As String = Split(sr.ReadToEnd, vbCrLf)
sr.Close()
Dim members(strData.Length - 1) As Member
For I As Integer = 0 To strData.Length - 1
Dim strTmp() As String = Split(strData(I), ",")
With members(I)
.FirstName = strTmp(1)
.LastName = strTmp(0)
.PhoneNumber = strTmp(2)
End With
Next
Return members
End Function
Private Sub SaveMembers(ByVal strFile As String, ByVal members() As Member)
Dim sw As New IO.StreamWriter(strFile)
For I As Integer = 0 To members.Length - 1
With members(I)
sw.WriteLine(.LastName & "," & .FirstName & "," & .PhoneNumber)
End With
sw.Close()
Next
End Sub
Private Sub DisplayMembers(ByRef listview As ListView, ByVal Members() As Member)
'Displays Members in a listbox
For I As Integer = 0 To Members.Length - 1
With Members(I)
listview.Items.Add(New ListViewItem(New String() {.LastName, .FirstName, .PhoneNumber}))
End With
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Configure the listview
ListView1.View = View.Details
ListView1.Columns.Add("Last Name", 120, HorizontalAlignment.Center)
ListView1.Columns.Add("First Name", 120, HorizontalAlignment.Center)
ListView1.Columns.Add("Phone Number", 120, HorizontalAlignment.Center)
Dim Members() As Member = LoadMembers("..\Members.txt")
DisplayMembers(ListView1, Members)
end Sub
Just add a ListView to the form and run
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|