[VB.NET]簡單的使用類別中的類別下的屬性

簡單的使用工廠和員工的例子去做類別和屬性的使用

先建立一個工廠的類別

Public Class Factory

    Private sAddress As String
    Private sTEL As String
    Private objEmp As New Employees

    Public Property Address() As String
        Get
            Return sAddress
        End Get
        Set(ByVal value As String)
            sAddress = value
        End Set
    End Property

    Public Property TEL() As String
        Get
            Return sTEL
        End Get
        Set(ByVal value As String)
            sTEL = value
        End Set
    End Property

    Public Property Employees() As Employees
        Get
            Return objEmp
        End Get
        Set(ByVal value As Employees)
            objEmp = value
        End Set
    End Property
End Class

 

再建立一個員工的類別

Public Class Employees
    Private bWork As Boolean
    Private sName As String


    Public Property Work() As Boolean
        Get
            Return bWork
        End Get
        Set(ByVal value As Boolean)
            bWork = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return sName
        End Get
        Set(ByVal value As String)
            sName = value
        End Set
    End Property

End Class

 

最後宣告使用

       Dim ft As New Factory
        ft.Address = "地球村台灣區台中路112號"

        '簡短寫法
        With ft.Employees
            .Name = "Tom"
            .Work = False
        End With
        '或用一般寫法
        ft.Employees.Name = "Tom"
        ft.Employees.Work = False