Hello,
My name is Josh I am in college and I am taking Intermediate VB.Net 2008 and I have been doing ok until now. I have been working on this problem for 2 weeks and still can not get it to do what it is suppose to do and it was suppose to be turned in last week. I do have a way to make it work but it doesn`t follow the guidelines and I would really like to learn what I am doing wrong here. So if anyone could help me it would be greatly appreciated it.
Here is the problem. I have a text file (MEMBERPHONES.TXT) the names and phone numbers from this text file should be read into an array of structures, and the names should be displayed in a list box when the form is loaded. There is a lot more instructions for this program that I already have figured out but this part has me stumped. Here is what I have so far PLEASE HELP and thank you in advance for looking at it.

Structure phone
        Dim name As String
        Dim phoneNum As Integer
    End Structure

    Dim phone1() As phone
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sw As IO.StreamWriter = IO.File.CreateText("MEMBERPHONES.TXT")
        sw.WriteLine("Langley, Judy")
        sw.WriteLine("321-1111")
        sw.WriteLine("Morgan,Fred")
        sw.WriteLine("222-2222")
        sw.WriteLine("Nixon, Mike")
        sw.WriteLine("333-3333")
        sw.WriteLine("Norton, Herbert")
        sw.WriteLine("444-4444")
        sw.WriteLine("Preiss, Carol")
        sw.WriteLine("555-5555")
        sw.WriteLine("Ree, Alice")
        sw.WriteLine("666-6666")
        sw.WriteLine("Sanchez, Carlos")
        sw.WriteLine("777-7777")
        sw.WriteLine("Smith, John")
        sw.WriteLine("888-8888")
        sw.Close()
        Dim numRecords As Integer
        numRecords = NumberOfRecords("MEMBERPHONES.TXT")
        Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")
        For index As Integer = 0 To numRecords - 1
            phone1(index).name = sr.ReadLine
            phone1(index).phoneNum = CInt(sr.ReadLine)
        Next
        sr.Close()
        Do While (sr.Peek <> -1)
            Dim i As Integer = 0
            ListBox1.Items.Add(phone1(i).name)
            i += 1
        Loop
    End Sub
    Function NumberOfRecords(ByVal MEMBERPHONES As String) As Integer
        Dim name As String
        Dim phoneNum As Integer
        Dim n As Integer = 0
        Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")
        Do While (sr.Peek <> -1)
            name = sr.ReadLine
            phoneNum = CInt(sr.ReadLine)
            n += 1
        Loop
        sr.Close()
        Return n
    End Function
End Class

Thanks again,
Josh

Edited 7 Years Ago by John A: added code tags

to read the details in a structure and adding to an arraylist you can use

Dim newArr As New ArrayList
        Dim sr() As String = IO.File.ReadAllText("MEMBERPHONES.TXT").Split(vbNewLine)
        For i As Integer = 0 To sr.Length - 1 Step 2
            Dim holdStruct As New phone
            holdStruct.name = sr(i)
            holdStruct.phoneNum = sr(i + 1)
            newArr.Add(holdStruct)
        Next

Hello,
My name is Josh I am in college and I am taking Intermediate VB.Net 2008 and I have been doing ok until now. I have been working on this problem for 2 weeks and still can not get it to do what it is suppose to do and it was suppose to be turned in last week. I do have a way to make it work but it doesn`t follow the guidelines and I would really like to learn what I am doing wrong here. So if anyone could help me it would be greatly appreciated it.
Here is the problem. I have a text file (MEMBERPHONES.TXT) the names and phone numbers from this text file should be read into an array of structures, and the names should be displayed in a list box when the form is loaded. There is a lot more instructions for this program that I already have figured out but this part has me stumped. Here is what I have so far PLEASE HELP and thank you in advance for looking at it.

Structure phone
        Dim name As String
        Dim phoneNum As Integer
    End Structure

    Dim phone1() As phone
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sw As IO.StreamWriter = IO.File.CreateText("MEMBERPHONES.TXT")
        sw.WriteLine("Langley, Judy")
        sw.WriteLine("321-1111")
        sw.WriteLine("Morgan,Fred")
        sw.WriteLine("222-2222")
        sw.WriteLine("Nixon, Mike")
        sw.WriteLine("333-3333")
        sw.WriteLine("Norton, Herbert")
        sw.WriteLine("444-4444")
        sw.WriteLine("Preiss, Carol")
        sw.WriteLine("555-5555")
        sw.WriteLine("Ree, Alice")
        sw.WriteLine("666-6666")
        sw.WriteLine("Sanchez, Carlos")
        sw.WriteLine("777-7777")
        sw.WriteLine("Smith, John")
        sw.WriteLine("888-8888")
        sw.Close()
        Dim numRecords As Integer
        numRecords = NumberOfRecords("MEMBERPHONES.TXT")
        Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")
        For index As Integer = 0 To numRecords - 1
            phone1(index).name = sr.ReadLine
            phone1(index).phoneNum = CInt(sr.ReadLine)
        Next
        sr.Close()
        Do While (sr.Peek <> -1)
            Dim i As Integer = 0
            ListBox1.Items.Add(phone1(i).name)
            i += 1
        Loop
    End Sub
    Function NumberOfRecords(ByVal MEMBERPHONES As String) As Integer
        Dim name As String
        Dim phoneNum As Integer
        Dim n As Integer = 0
        Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")
        Do While (sr.Peek <> -1)
            name = sr.ReadLine
            phoneNum = CInt(sr.ReadLine)
            n += 1
        Loop
        sr.Close()
        Return n
    End Function
End Class

Thanks again,
Josh

Josh,

I believe the reason you are having challenges is the technique.

A Structure can be read and written at once, here is a good example of doing just that : http://addressof.com/blog/archive/2003/05/08/180.aspx

Hope this helps.

Thats an odd struture for a data file (two lines per record). Usually its one line of data per record with some type of delimiter or fixed length to define each of the fields.

If your sure that is the right file structure, GeekByChoiCe gives a good example of how to input the file. Also I would add that you could add your listbox items right in the same loop rather then adding a second loop like you have done in the original example.

This article has been dead for over six months. Start a new discussion instead.