Vb.net Question

I am getting error when accessing variable on another module?

I have a module that is getting values from a datagrid and puts in the tags from each rows into the array of strings. I'm calling that array string on another module but i'm getting object not set to instance of an object. Why? What i'm trying to accomplish is combine all the tag into a array of strings or collection and be able to access it on another module.

'my main module
Public Class myMainModule
Public Shared myArray() As String

......
.......
Public sub doSomething()
Dim myArray As New List(Of String)
For Each row As DataGridViewRow In mydatagrid.Rows
If row.Cells("mycheckbox").Value = True Then
myArray.Add(row.Tag)
End If
Next
End Sub
End Class


'....then i'm calling it from another module:

Public Class myOtherModule
Public sub doit()
For Each value As String In myMainModule.myArray
Debug.Print(value)
Next
End Sub
End Class

Answer

You need to initialize your Array before you try to call it. Currently it is Nothing.

Public Class MyMainModule
    Public Shared MyArray() As String

    Public Shared Sub DoSomething()
        Dim myList As New List(Of String)
        For Each row As DataGridViewRow In mydatagrid.Rows
         If row.Cells("mycheckbox").Value = True Then
             myList.Add(row.Tag)
         End If
        Next

        MyArray = myList.ToArray()
    End Sub
End Class

Public Class MyOtherModule
    Public Sub Foo()
        MyMainModule.DoSomething() 
        For Each value As String In MyMainModule.MyArray
            Debug.Print(value)
        Next
    End Sub
End Class

The other thing too is that you need to watch out for naming. I believe you got confused because you had a field called myArray, but also had a local variabled called myArray. You were working with the local variable that you newed up as List(Of T), not an array.