Results 1 to 4 of 4

Thread: [RESOLVED] Data to an array from TXT file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    NZ
    Posts
    178

    Resolved [RESOLVED] Data to an array from TXT file

    Hi folks. I am trying to sort a list of phone directories stored in a TXT via a bubblesort using a tempArray. The problem I have that the code breaks on the line that supposed to store the data in the array. This part of a bigger exercise but I need to list data alpha/numeric ascending.

    VB Code:
    1. Dim sr As StreamReader
    2.     Dim sw As StreamWriter
    3.     Dim temp() As String
    4.     Dim prompt As String
    5.     Dim counter, i, j As Integer
    6.     fn = "PHONELISTS.TXT" 'File with phone directories
    7.     sr = File.OpenText(f & fn)
    8.     counter = 0
    9.     Do While (sr.Peek <> -1)
    10.       counter += 1
    11.      :confused: [COLOR=Blue][B]temp(i) = sr.ReadLine [/B] [/COLOR]
    12.     Loop
    13.     sr.Close()
    14.     OpenDir(fn, counter)
    15.   End Sub
    Last edited by lionelnz; May 10th, 2006 at 04:58 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data to an array from TXT file

    You haven't created an array object. This line:
    VB Code:
    1. Dim temp() As String
    declares an array variable. You cannot create an array object without specifying how many elements it will contain because arrays are fixed-size. Even if you had created an array object you couldn't simply keep adding elements to it beyond its length. I suggest that you follow the Start VB.NET link in my signature and read the section on arrays. You should find that it enlightens you considerably regarding arrays in .NET.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Data to an array from TXT file

    You can always resize with something like this:

    VB Code:
    1. dim sz as integer
    2. dim arr() as string
    3.  
    4. sz+=1
    5. redim preserve arr(sz)
    6. arr(sz)="Whatever"

    There are other ways, and there may well be simpler ways, but not using a straight array (at least not one that I know of).

    So, why bubble sort? With all the options out there, why choose the slowest one?
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    NZ
    Posts
    178

    Re: Data to an array from TXT file

    Quote Originally Posted by Shaggy Hiker
    So, why bubble sort? With all the options out there, why choose the slowest one?
    Cheers I do have to do a bubblesort & this is part of a bigger project.

    I have to search for & create phone directories(PD) and the phone directories are in a sequential TXT file. If PD not exist then create it then I have to add & delete names to the various directories. All data have to be in ascending alpha order as well.

    This is killing me as I think I have system problems, I have trouble getting on the net, I am doing home study & of course the text has minor glitches so I need al the help I can get!!!

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