Results 1 to 3 of 3

Thread: array problems

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    145

    array problems

    im having some problems with arrays. the error says something about a null reference. this is my code:

    Code:
    dim inventory as array
    inventory(1)="Axe"
    inventory(2)="Wood"
    etc
    does anyone have any idea what the problem could be?

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

    Re: array problems

    That first line doesn't create an array. It simply declares a variable of type Array. The variable can refer to an Array object but, in your code, it refers to Nothing, i.e. no object.

    The first thing to note is that you almost never use the Array type directly. The Array class is the base type for all arrays but you should always create an array of the type you need to store your objects. You're storing string so you should create a String array:
    vb.net Code:
    1. Dim arr1 As String()
    2. Dim arr2() As String
    Both those lines do the same thing: they declare a variable that can refer to a String array object. In both cases though, the variable refers to Nothing.

    In order to create an array object you MUST specify the size of the array. Arrays cannot be resized, so you can't just create one and start setting elements at arbitrary indexes. You must specify the size of the array so that the system can set aside enough memory to store that number of elements. It is an error to set or get an element beyond the bounds you specify when you create the array. Note that, in VB, you specify the upper bound of the array when you create it. The upper bound is the highest index. As arrays are zero-based the highest index is always one less than the length:
    vb.net Code:
    1. Dim arr1(9) As String
    2. Dim arr2 As String = New String(9) {}
    Both of those lines do the same thing: they declare a variable that can refer to a String array AND they create a String array object with 10 elements AND they assign that object to the variable. The 10 elements are accessed using indexes 0 to 9.

    So, once you have created an array object and assigned it to a variable, then you can access the array through that variable to get and set elements.

    Now, I said earlier that arrays cannot be resized. That is strictly true. Once you create an array object you cannot change the number of elements it contains. The language and the Framework give the appearance that you can resize an array with the ReDim statement and the Array.Resize method. In actual fact, both of those techniques actually create new array objects and copy the contents of the old array to it. With the ReDim statement you must use the Preserve key word to affect that copy. Reallocating memory for arrays and copying elements can be expensive, especially for large arrays, so it's something you want to do as little as possible. Usually you only use arrays where the number of elements will not change. If you want to create a list that can grow and shrink dynamically you should use a collection, specifically the List class.
    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

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    145

    Re: array problems

    thanks alot, that was very detailed and answered all questions and uncertainties that i was having. thankyou

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