Possible to have a class that contains an array?

x3haloed

New member
Joined
Jan 8, 2009
Messages
2
Programming Experience
1-3
I have two classes, and I want one of them to be able to hold an array of the other.
Code:
VB.NET:
Public Class CTerm
    Public Text As String
    Public Standard As Boolean

    Public Overrides Function ToString() As String
        Return Text
    End Function
End Class

Public Class CTermMatch
    Public arrTerms() As CTerm

    Public Overrides Function ToString() As String
        Dim strTemp As String

        strTemp = ""

        For Each pTerm In arrTerms
            strTemp = strTemp & pTerm.ToString & " = "
        Next

        Return strTemp
    End Function
End Class
Is this possible?

When I try to equate arrTerms() with another array, it throws this exception:
VB.NET:
System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
 
Yes it is possible but, in your case, you've never actually created an array. You only declared a variable that can refer to an array. You've never assigned an array object to that variable so it is Nothing, also known as a null reference, hence the NullReferenceException. You're trying to access a member of an object that doesn't exist. To create an array you must specify it's size. You haven't specified a size so you haven't created an array.
 
Thanks for the reply!
I figured out what was wrong. Like an idiot, I forgot to instantiate arrTerms() as a New CTerm.
 
Back
Top