|
-
May 19th, 2017, 07:05 PM
#1
Thread Starter
Fanatic Member
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?
-
May 19th, 2017, 08:53 PM
#2
Re: Array code errors
You need to understand the difference between a variable and an object. This code:
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:
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:
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:
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.
-
May 19th, 2017, 09:00 PM
#3
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:
'Create a collection object by invoking a constructor. 'The collection is initially empty, i.e. has a Count of 0. Dim mb As New List(Of String) 'Add an item to the collection. 'The collection grows dynamically and now has a Count of 1. 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.
-
May 20th, 2017, 11:45 AM
#4
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|