title: How do I declare arry of objects outside a soubroutine in VB.net
body: <p>I wrote a code to demonstrate the issue:</p>
  <pre><code>Public Class Form1
  
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          doSomething()
      End Sub
  
      Dim controlArr() As Object = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4, CheckBox1, CheckBox2, CheckBox3, CheckBox4}
      Private Sub doSomething()
          Dim testStr As String = &quot;&quot;
          For Each control In controlArr
              Select Case control.GetType
                  Case GetType(NumericUpDown)
                      control.value = 1
                  Case GetType(CheckBox)
                      control.checked = True
              End Select
          Next
      End Sub
  End Class
  </code></pre>
  <p>When I run the code I receive Null Referenece Exception &quot;Object reference not set to an instance of an object&quot;, the error disapears when I declare the <code>controlArr</code> array inside <code>DoSomething</code> subroutine. Anyway I would prefer having it declared outside since I am using it in many functions. I would like to understand it better so if you provided me with a topic I could read up on I would be very grateful. Thank you very much for your help.</p>