Check for an instance of an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JIPS

    Check for an instance of an array?

    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.NullRef erenceException ' 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(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    colChk.Add(Chec kBox1)
    colChk.Add(Chec kBox2)
    colChk.Add(Chec kBox3)
    colChk.Add(Chec kBox4)

    End Sub

    Private Sub CheckBox1_Check edChanged(ByVal sender As Object, ByVal e As
    System.EventArg s) 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.Lengt h
    TextBox1.Text += mstrItems(intx - 1)

    Next

    End Sub
    End Class


  • Kerry Moorman

    #2
    RE: Check for an instance of an array?

    JIPS,

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

    Kerry Moorman


    "JIPS" wrote:
    [color=blue]
    > 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.NullRef erenceException ' 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(ByVa l sender As System.Object, ByVal e As
    > System.EventArg s) Handles MyBase.Load
    >
    > colChk.Add(Chec kBox1)
    > colChk.Add(Chec kBox2)
    > colChk.Add(Chec kBox3)
    > colChk.Add(Chec kBox4)
    >
    > End Sub
    >
    > Private Sub CheckBox1_Check edChanged(ByVal sender As Object, ByVal e As
    > System.EventArg s) 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.Lengt h
    > TextBox1.Text += mstrItems(intx - 1)
    >
    > Next
    >
    > End Sub
    > End Class
    >
    >
    >[/color]

    Comment

    • JIPS

      #3
      Re: Check for an instance of an array?

      Kerry,

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

      Jason

      "Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.com> wrote in message
      news:276801DE-DCBD-4E6E-B977-5455D5A4BB06@mi crosoft.com...[color=blue]
      > JIPS,
      >
      > If Not mstrItems Is Nothing Then
      > For intx = 0 To mstrItems.Lengt h - 1
      > TextBox1.Text += mstrItems(intx - 1)
      > Next
      > End If
      >
      > Kerry Moorman
      >
      >
      > "JIPS" wrote:
      >[color=green]
      >> 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.NullRef erenceException ' 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(ByVa l sender As System.Object, ByVal e As
      >> System.EventArg s) Handles MyBase.Load
      >>
      >> colChk.Add(Chec kBox1)
      >> colChk.Add(Chec kBox2)
      >> colChk.Add(Chec kBox3)
      >> colChk.Add(Chec kBox4)
      >>
      >> End Sub
      >>
      >> Private Sub CheckBox1_Check edChanged(ByVal sender As Object, ByVal e
      >> As
      >> System.EventArg s) 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.Lengt h
      >> TextBox1.Text += mstrItems(intx - 1)
      >>
      >> Next
      >>
      >> End Sub
      >> End Class
      >>
      >>
      >>[/color][/color]


      Comment

      Working...