Results 1 to 5 of 5

Thread: [RESOLVED] Making Arrays larger

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    40

    Resolved [RESOLVED] Making Arrays larger

    For this question I am using VB.NET 2003.
    I know to make an array larger you use the ReDim Preserve "array" command, but I was wondering how to determine if my original array is too small.
    If I make an array 50 "blocks" long, and I am reading from a file that has 55 lines, then how do I know that the array is too small and therefore should make it 100 blocks long, etc.
    Thanks

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

    Re: Making Arrays larger

    The following code doubles the length of the array each time, which is what a collection does with its Capacity. You may choose to grow the array by a fixed amount each time instead.
    VB Code:
    1. Dim arr(50 - 1) As String
    2.         Dim sr As New IO.StreamReader("C:\MyFile.txt")
    3.         Dim linesRead As Integer = 0
    4.  
    5.         Do Until sr.Peek() = -1
    6.             If linesRead = arr.Length Then
    7.                 'We have filled the array so enlarge it.
    8.                 ReDim Preserve arr(arr.Length * 2 - 1)
    9.             End If
    10.  
    11.             arr(linesRead) = sr.ReadLine()
    12.         Loop
    13.  
    14.         'Remove unused elements.
    15.         ReDim Preserve arr(linesRead - 1)
    16.  
    17.         sr.Close()
    You might also look at using a Specialized.StringCollection instead, depending on the circumstances. It is a collection and thus handles the growing and shrinking automatically.
    Last edited by jmcilhinney; Mar 30th, 2006 at 11:55 PM.
    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
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Making Arrays larger

    VB Code:
    1. Dim MyArray(55) As String
    2.         Dim tr As StreamReader = New StreamReader("c:\MyFile.txt")
    3.         Dim intLines As Integer = UBound(Split(tr.ReadToEnd(), vbNewLine)) + 1
    4.  
    5.         If intLines > UBound(MyArray) Then
    6.             ReDim Preserve MyArray(intLines)
    7.         End If
    8.  
    9.         tr.Close()
    Last edited by jcis; Mar 31st, 2006 at 01:40 AM.

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    40

    Re: Making Arrays larger

    Thats great! Just what I was looking for.
    Thanks again

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

    Re: Making Arrays larger

    Quote Originally Posted by jcis
    VB Code:
    1. Dim MyArray(55) As String
    2.         Dim tr As StreamReader = New StreamReader("c:\MyFile.txt")
    3.         Dim intLines As Integer = UBound(Split(tr.ReadToEnd(), vbNewLine)) + 1
    4.  
    5.         If intLines > UBound(MyArray) Then
    6.             ReDim Preserve MyArray(100)
    7.         End If
    8.  
    9.         tr.Close()
    If you're going to do it that way then you may as well jsut do this:
    VB Code:
    1. Dim myArray As String() = System.Text.RegularExpressions.Regex.Split(myStreamReader.ReadToEnd(), Environment.NewLine)
    No need to ever specify the size of the array because you aren't creating it explicilty. You can also use the Split Runtime function but I prefer to avoid Runtime functions. I couldn't really say which is more efficient in this case though.
    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

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