Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Bookstore...
 
Feedback
Tip Jar
 
XML RSS Feed
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
 
  Tip: .NET Empty Arrays  
 
 

In VB .NET, you cannot use an array's properties and methods until you instantiate it. For example, the following code declares an array. The two Debug.WriteLine statements that follow fail because the array has not been created.

    Dim values() As Integer
    Debug.WriteLine(values.Length)
    Debug.WriteLine(values.GetLowerBound(0))

You can see if the array is Nothing to tell if it has been created yet.

Sometimes, however, the code would be more consistent if you could use Length, GetLowerBound, and GetUpperBound to loop over the empty array. You can do that if you use the following statement to allocate the array with no items in it.

    Dim values(-1) As Integer

Now the array exists, Length returns 0, GetLowerBound returns 0, and GetUpperBound returns -1.

 

Copyright © 1997-2008 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated