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

Dani AI

Generated

Quick diagnosis and a safe pattern to follow.

The code posted has a few common pitfalls: the array of structures is never sized before you index it, phone numbers are treated as integers even though they contain dashes, the file is being overwritten in Form_Load (so tests are unstable), and a StreamReader is used after being closed. Those issues cause IndexOutOfRange, FormatException, and logic errors. and were right to point at alternative input strategies; 's pointer about structure I/O is useful if binary or block reads are required.

A practical, robust approach:

  • Read the file line-by-line with a Using block so the reader is always closed correctly.
  • Pair lines (name then phone). If the file might be malformed, defensively handle a missing phone line.
  • Store phone numbers as strings to preserve formatting. Validate or normalize only if needed.
  • Populate the ListBox while you read, or collect into a typed List(Of T) and bind/show afterwards.
  • If the assignment mandates an array of structures, count records first and then ReDim the array to size before filling it.

Example pattern (VB.NET) that is safe and different from earlier snippets:

Private Class PersonPhone
    Public Property FullName As String
    Public Property PhoneNumber As String
End Class

' In Form_Load:
Dim records As New List(Of PersonPhone)
Using r As New IO.StreamReader("MEMBERPHONES.TXT")
    While Not r.EndOfStream
        Dim n = r.ReadLine()
        Dim p = If(Not r.EndOfStream, r.ReadLine(), String.Empty)
        If String.IsNullOrWhiteSpace(n) Then Continue While
        records.Add(New PersonPhone With {.FullName = n.Trim(), .PhoneNumber = p.Trim()})
    End While
End Using

For Each it In records
    ListBox1.Items.Add(it.FullName)
Next

Troubleshooting checklist: enable Option Strict On to catch type issues; verify the file path (Application.StartupPath vs project folder); handle odd-numbered line counts; and avoid creating test data inside Form_Load unless intentional. This will make the program predictable and easier to grade.

Recommended Answers

All 4 Replies

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 :

Hope this helps.

Thank you I will try this out.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.