Check for an instance of an array?

J

JIPS

I'm looking for how one tests for the existance of an array. After I clear
it, and then I go to use the array, if it hasn't had anything added to it, I
receive the error
"An unhandled exception of type 'System.NullReferenceException' occurred in
Reports.exe
Additional information: Object reference not set to an instance of an
object."

Just to make things clear, below is a simple example of code.

Any help is much appreciated.

thanks,
Jason
------------------------------------------------------------------
Public Class frmMain
Inherits System.Windows.Forms.Form
Dim mstrItems() As String
Dim colChk As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

colChk.Add(CheckBox1)
colChk.Add(CheckBox2)
colChk.Add(CheckBox3)
colChk.Add(CheckBox4)

End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CheckBox1.Click, CheckBox2.Click, CheckBox3.Click,
CheckBox4.Click

Dim intx As Integer
Dim curChk As CheckBox

TextBox1.Text = ""

Erase mstrItems

For intx = 1 To colChk.Count
curChk = colChk(intx)
If curChk.Checked Then
ReDim Preserve mstrItems(intx - 1)
mstrItems(intx - 1) = curChk.Text
End If
Next

'here is where the error occurs if nothing has been added
'to the array, how do I check if the array has been Redim'd?

For intx = 1 To mstrItems.Length
TextBox1.Text += mstrItems(intx - 1)

Next

End Sub
End Class
 
G

Guest

JIPS,

If Not mstrItems Is Nothing Then
For intx = 0 To mstrItems.Length - 1
TextBox1.Text += mstrItems(intx - 1)
Next
End If

Kerry Moorman
 
J

JIPS

Kerry,

Works great, thanks a lot! I'm just learning, so thank you for taking the
time to answer.

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top