VB.net计算在命名空间中创建的类的数量

时间:2016-01-07 18:34:56

标签: vb.net class namespaces

我正在尝试学习使用命名空间,我目前使用命名空间创建了2个项目。但是在将来正确实施时,将使用命名空间创建x个项目。

如何计算存在的“Cilinders”数量?

Public Class Form1

    ' Test for using namespace
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        ' Create cilinder 1
        Dim Cilinder_1 As New BODY_NAMESPACE.Body_Cilinder
        Cilinder_1.Index = 1
        Debug.Print(Cilinder_1.Index)

        ' Create cilinder 2
        Dim Cilinder_2 As New BODY_NAMESPACE.Body_Cilinder
        Cilinder_2.Index = 2
        Debug.Print(Cilinder_2.Index)

    End Sub

End Class

Namespace BODY_NAMESPACE
    Class Body_Cilinder
        Private _Index As Integer

        Public Property Index() As Integer
            Get
                Index = _Index
            End Get
            Set(ByVal value As Integer)
                _Index = value
            End Set
        End Property
    End Class
End Namespace

1 个答案:

答案 0 :(得分:-1)

经过一番挖掘后发现了解决方案。我使用了一个糟糕的搜索词。

Public Class Form1

' Test for using namespace
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    ' Create cilinder 1
    Dim Cilinder_1 As New BODY_NAMESPACE.Body_Cilinder
    Cilinder_1.Index = 1
    Debug.Print(Cilinder_1.Index)

    ' Create cilinder 2
    Dim Cilinder_2 As New BODY_NAMESPACE.Body_Cilinder
    Cilinder_2.Index = 2
    Debug.Print(Cilinder_2.Index)

    ' Count the cilinders
    Debug.Print(BODY_NAMESPACE.Body_Cilinder.Count)

End Sub

End Class

Namespace BODY_NAMESPACE

' Class that defines all the Cilinders properties for easy access.
Class Body_Cilinder

    ' Counter to determinate the cilinder quantity required.
    Public Shared Count As Integer = 0

    Private _Index As Integer
    Private _Height As Double

    ' Cilinder Index number As Integer for finding the position.
    Public Property Index() As Integer
        Get
            Index = _Index
        End Get
        Set(ByVal value As Integer)
            _Index = value
        End Set
    End Property

    ' Cilinder Height to define the cilinder height.
    Public Property Height() As Double
        Get
            Height = _Height
        End Get
        Set(value As Double)
            _Height = value
        End Set
    End Property

    ' Add Cilinder count every-time a new "Body Cilinder" instance Is created
    Public Sub New()
        Count = Count + 1
    End Sub
End Class

End Namespace