Results 1 to 2 of 2

Thread: Syntax HELP: Array/CSV Split

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    1

    Question 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.

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    VB Code:
    1. Private Structure Member
    2.         Dim FirstName As String
    3.         Dim LastName As String
    4.         Dim PhoneNumber As String
    5.     End Structure
    6.  
    7.     Private Function LoadMembers(ByVal strFile As String) As Member()
    8.         Const ErrMsg As String = "Either no file has yet been created or the file is not where it was expected."
    9.  
    10.         If IO.File.Exists(strFile) = False Then
    11.             Throw New IO.FileNotFoundException(ErrMsg, strFile)
    12.             Exit Function
    13.         End If
    14.  
    15.         Dim sr As New IO.StreamReader(strFile)
    16.  
    17.         Dim strData() As String = Split(sr.ReadToEnd, vbCrLf)
    18.  
    19.         sr.Close()
    20.  
    21.         Dim members(strData.Length - 1) As Member
    22.  
    23.         For I As Integer = 0 To strData.Length - 1
    24.             Dim strTmp() As String = Split(strData(I), ",")
    25.             With members(I)
    26.                 .FirstName = strTmp(1)
    27.                 .LastName = strTmp(0)
    28.                 .PhoneNumber = strTmp(2)
    29.             End With
    30.         Next
    31.  
    32.         Return members
    33.  
    34.     End Function
    35.  
    36.     Private Sub SaveMembers(ByVal strFile As String, ByVal members() As Member)
    37.  
    38.  
    39.         Dim sw As New IO.StreamWriter(strFile)
    40.  
    41.         For I As Integer = 0 To members.Length - 1
    42.             With members(I)
    43.                 sw.WriteLine(.LastName & "," & .FirstName & "," & .PhoneNumber)
    44.             End With
    45.  
    46.             sw.Close()
    47.         Next
    48.     End Sub
    49.  
    50.     Private Sub DisplayMembers(ByRef listview As ListView, ByVal Members() As Member)
    51.         'Displays Members in a listbox
    52.  
    53.         For I As Integer = 0 To Members.Length - 1
    54.             With Members(I)
    55.                 listview.Items.Add(New ListViewItem(New String() {.LastName, .FirstName, .PhoneNumber}))
    56.             End With
    57.         Next
    58.     End Sub
    59.  
    60.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    61.  
    62.         'Configure the listview
    63.  
    64.         ListView1.View = View.Details
    65.         ListView1.Columns.Add("Last Name", 120, HorizontalAlignment.Center)
    66.         ListView1.Columns.Add("First Name", 120, HorizontalAlignment.Center)
    67.         ListView1.Columns.Add("Phone Number", 120, HorizontalAlignment.Center)
    68.  
    69.         Dim Members() As Member = LoadMembers("..\Members.txt")
    70.         DisplayMembers(ListView1, Members)
    71.     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
  •  



Click Here to Expand Forum to Full Width