Class as property of another class

SDavied

Active member
Joined
Aug 26, 2006
Messages
33
Location
Kansas
Programming Experience
3-5
I need to use a class as a property of another class. Not sure what's wrong with this:

//Employee class has a Tab as a member
VB.NET:
<Serializable()> Public Class Employee
    Private _class As EmployeeClass
    Private _name As String
    Private _password As String
    Private _tab As Tab

    Public Enum EmployeeClass
        MANAGER
        NON_MANAGER
    End Enum

    Public Sub New(ByVal c As EmployeeClass)
        _class = c
        _tab = New Tab(Tab.TabType.EMPLOYEE)
    End Sub

    Public ReadOnly Property EClass() As EmployeeClass
        Get
            Return _class
        End Get
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal Value As String)
            _name = Value
            _tab.Name = _name
        End Set
    End Property

    'password is encoded when set
    Public Property PassWord() As String
        Get
            Return _password
        End Get
        Set(ByVal Value As String)
            Dim pe As New PasswordEncoder
            _password = pe.Encode(Value)
        End Set
    End Property

    Public Property Tab() As Tab
        Get

        End Get
        Set(ByVal Value As Tab)

        End Set
    End Property
//Tab class
VB.NET:
<Serializable()> Public Class Tab

#Region "Private Variables"
    Private _type As TabType
    Private _name As String
    Private _amount() As Double
    Private _start_date As Date
#End Region

#Region "Properties"

    Public Enum TabType
        EMPLOYEE
        CUSTOMER
    End Enum

    Public ReadOnly Property TType() As TabType
        Get
            Return _type
        End Get
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal Value As String)
            _name = Value
        End Set
    End Property

    Public Property Amount(ByVal i As Integer) As Double
        Get
            Return _amount(i)
        End Get
        Set(ByVal Value As Double)
            _amount(i) = Value
        End Set
    End Property

    Public Property StartDate() As Date
        Get
            Return _start_date
        End Get
        Set(ByVal Value As Date)
            _start_date = Value
        End Set
    End Property
#End Region

#Region "Constructor"

    Public Sub New(ByVal _tabType As TabType)
        _start_date = Date.Today
        _type = _tabType
        If _type = TabType.EMPLOYEE Then
            _amount = New Double() {0, 0, 0, 0, 0, 0}
        Else
            _amount = New Double() {0}
        End If

    End Sub

    Public Function GetTotalAmount() As Double
        Dim result As Double

        For Each i As Double In _amount
            result += i
        Next i

        Return result
    End Function
#End Region

End Class
When I try to test this by trying to set the Tab's date property:

<instance_of_employee>.Tab.StartDate = Date.Today

I get an error stating that object reference not set to an object. When the sub New() of Employee is called, an instance of Tab is also instantiated. Not sure what's going on here. Any idea's.

Thanks, in advance,
Scott D.
 
Nevermind. I'm an idiot. All I had to do was make the following change:


VB.NET:
 Public ReadOnly Property Tab() As Tab
        Get
            Return _tab
        End Get
    End Property

Now it works flawlessly.
 
Back
Top