Results 1 to 4 of 4

Thread: Array code errors

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Array code errors

    I have the following code:

    Code:
            Dim mb() As String
            mb(0) = "xyz"
    I get the syntax message "Variable 'mb' is used before it has been assigned a value" on the second line.

    So I change it to

    Code:
            Dim mb() As String = Nothing
            mb(0) = "xyz"
    and the above message goes away but I get the run-time error "Object reference not set to an instance of an object".

    What to do?

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

    Re: Array code errors

    You need to understand the difference between a variable and an object. This code:
    vb.net Code:
    1. Dim mb() As String
    declares a String array variable but does not create a String array object. All variables are Nothing by default so the compiler is warning you that your code is trying to use a variable that has never had an object assigned to it. This code:
    vb.net Code:
    1. Dim mb() As String = Nothing
    does assign to the variable and so prevents the compiler warning but, because you're still not assigning an actual String array object to the variable, you end up with a NullReferenceException at run-time, which is exactly why the compiler was warning you in the first place.

    In order to be able to set the value of the element at index 0 in an array, you have to actually have an array. You don't. In the second code snippet you are explicitly say "my variable refers to no object" so why would you expect to be able to set an element of an array that doesn't exist? The first code snippet is basically the same except that the variable is implicitly Nothing rather than explicitly.

    Just like for any reference type, if you want to have an array to use then you have to create one. In order to create an array, you MUST specify its size, either implicitly or explicitly. The most common way to do that is by specifying an upper bound like so:
    vb.net Code:
    1. Dim mb(upperBound) As String
    That will create an array with (upperBound + 1) elements at indexes 0 to upperBound. That is specifying the size explicitly. You can specify the size implicitly by initialising the elements of the array, e.g.
    vb.net Code:
    1. Dim mb As String() = {"xyz", "hello", "world"}
    That array now has 3 elements at indexes 0 to 2.

    When dealing with reference types, i.e. classes, you don't have an object by default and it's up to you to create one. For most classes, that means invoking a constructor or calling a method that returns an object. Arrays are special cases with inbuilt language support so you have the extra option of creating an object by specifying its size. If you haven't specified the size of an array one way or another then you know that you haven't created an array object.
    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 jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array code errors

    I forgot to add that, if you don't actually know how many elements your array should have then don't create an array. In that case, use a collection instead. Collection types will still be classes so you still have to create an object, but the collection can grow and shrink dynamically, e.g.
    vb.net Code:
    1. 'Create a collection object by invoking a constructor.
    2. 'The collection is initially empty, i.e. has a Count of 0.
    3. Dim mb As New List(Of String)
    4.  
    5. 'Add an item to the collection.
    6. 'The collection grows dynamically and now has a Count of 1.
    7. mb.Add("xyz")
    Note that collections generally use arrays internally but they manage them in a relatively efficient way that would require you to write a significant amount of code to replicate if you were to use arrays and "resize" them yourself. I use the quotes because you can't actually resize an array but must actually create a new array object of the desired size and copy the elements between the two. Much of that can be automated but it's still a pain to do efficiently.

    If you really need an array for some particular purpose, you can always call ToArray on the collection to generate an array containing the current items.
    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

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Array code errors

    Noted. Thanks for the information.

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