Skip navigation.

Blog

Passing an Empty Array in VB.Net

When needing to pass arrays around between methods, I had always initialised them as Nothing so that I knew when they were completely empty. i.e.

Dim arrayVar() As String = Nothing

This is fine, but does then mean doing an extra check before working with the array. i.e.

If arrayVar IsNot Nothing Then
    For i As Integer = 0 To arrayVar.GetUpperBound(0)
        'do other work with the array, etc
    Next
End If

The tip, is to initialise the array as size -1 instead of Nothing, i.e.

Dim arrayVar(-1) As String

This means that you then don't need the extra IsNot Nothing check, saving two lines of code every time the array is used, and allowing you to have coffee a few seconds earlier than you otherwise would!

Source: Thanks to Dayba for the original tip and allowing me to get to coffee quicker!

By on March 4, 2009 | Permalink | Comment


Reader Comments

Skip to form

There are currently no comments about this article.


Comment on This Article:

Your Name:
Your Email Address:
 

Your Email Address will not be made public,
but the MD5 hash of it will be used to display your Gravatar.
Comment:
All HTML, except <i>, <b>, <u> will require your comment to be moderated before it is publicly displayed.
 
If you would like your own avatar displayed, read about comment avatars.