我正在尝试学习使用命名空间,我目前使用命名空间创建了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
答案 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