Results 1 to 12 of 12

Thread: Changing Upperbound

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Changing Upperbound

    Is it possible to change the upperbound of an array once it has been instantiated? I want to change the upperbound in an array so it matches the number of items that hold a value.

    Cheers.

  2. #2

    Re: Changing Upperbound

    Just create an array based on the number of items holding a value....I don't think its possible to actually change the array like you want.

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Changing Upperbound

    While you can change the size of an array by using ReDim or ReDim Preserve keywords, resizing an array is generally costly (in term of CPU cycles). The better approach is to use some kind of collection based type, such as List(Of T).
    Anyway, here is how you change the size of an array (thus changing its upperbound)
    Code:
    'Declare an array with 1 element
    Dim arr(0) As Integer
    
    'Change that array to 5 elements discarding the existing values
    ReDim arr(4)
    
    'Change that array again to 10 elements keeping the existing values
    ReDim Preserve arr(9)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: Changing Upperbound

    Redim works great. Had to assign it to a new array first using dim though as it wouldn't work on a private decalred array

  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Changing Upperbound

    Quote Originally Posted by stanav View Post
    While you can change the size of an array by using ReDim or ReDim Preserve keywords, resizing an array is generally costly (in term of CPU cycles). The better approach is to use some kind of collection based type, such as List(Of T).
    [/code]
    You'd better check and see what a list does when you add items to it if you haven't declared a size - It redimensions an array... Even so, using a list is still a good idea for collections of objects which one is going to manipulate.

    (Actually, that's not strictly true. If there isn't enough room in the array, then it creates a new array of the required size and copies the existing values to that new array - probably what Redim Preserve actually does).
    Last edited by SJWhiteley; Jun 15th, 2009 at 02:24 PM.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

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

    Re: Changing Upperbound

    That's not quite it, either. A list starts out with some size. When you add an item beyond that size, it doubles the size. Each time it resizes, it doubles. This reduces the frequency of resizing.

    You can also add arrays into Lists quite easily, and turn Lists back into arrays. Therefore, there is no good reason to use an array that doesn't have a fixed size.

    Note that Redim will erase the array, while Redim Preserve will not. Also note that Redim will only work on the last dimension of an array, so you can't redimension multidimensional arrays without extensive copying.

    Not sure what you meant by needing to copy it, something wasn't quite right in that statement, as there is no issue with redimensioning private arrays. I did it extensively before moving to 2005 where the List (of T) was introduced.
    My usual boring signature: Nothing

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Changing Upperbound

    No, a list doesn't double its capacity. When you add an item to it, it only ensures there's is enough capacity. If there isn't it increases the size by one. So, each time you add an item to a list it'll recreate the internal array. The default capacity is zero for the List object.

    But lets make it clear, we are talking about the System.Collections.Generic.List(of T) object, aren't we?
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

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

    Re: Changing Upperbound

    Yes it does double capacity. There may be an upper limit, but test this out to see:
    Code:
    Dim mL As New List(Of String)
            Dim x As Integer
    
            For x = 0 To 33
                mL.Add(mL.Capacity.ToString)
            Next
    My usual boring signature: Nothing

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Changing Upperbound

    Shaggy is right, it doubles when the Count = Capacity. (Although it starts at 4 on the first added item). SJWhiteley, if it were like this:

    Quote Originally Posted by SJWhiteley
    When you add an item to it, it only ensures there's is enough capacity. If there isn't it increases the size by one.
    Then that would mean the Count + 1 = Capacity, which is not True. The easiest way to see this is:

    vb.net Code:
    1. Dim myList As New List(Of String)
    2.         Dim myCaps As New List(Of Integer)(New Integer() {0})
    3.         For x As Integer = 0 To 10000
    4.             myList.Add(x.ToString())
    5.             If Not myCaps.Contains(myList.Capacity) Then
    6.                 myCaps.Add(myList.Capacity)
    7.             End If
    8.         Next
    9.         For Each i As Integer In myCaps
    10.             Debug.WriteLine(i.ToString())
    11.         Next
    Last edited by ForumAccount; Jun 15th, 2009 at 05:02 PM.

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

    Re: Changing Upperbound

    I thought my way of seeing it was easier than that, but I didn't output anything.
    My usual boring signature: Nothing

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

    Re: Changing Upperbound

    Quote Originally Posted by ForumAccount View Post
    Shaggy is right, it doubles when the Count = Capacity. (Although it starts at 4 on the first added item).
    As another aside, it's worth noting that 4 is just the default initial size and you shouldn't accept that if you know for a fact that it will end up bigger. For instance, if you know for a fact that your List will end up containing at least 10 items then you should should specify that capacity when you create it:
    vb.net Code:
    1. Dim myList As New List(Of Object)(10)
    That way the internal array won't need to be resized until the 11th item is added, at which point the capacity is doubled to 20. If the count never passes 10 then no resizing is needed at all.
    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

  12. #12
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Changing Upperbound

    Yeah, you are right. The initial capacity is zero, the first item added it goes to 4, then goes to the current size * 2. I had tested by adding 8 items...
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

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