我正在尝试创建一个数据结构CityPosition
,其中包含3个变量:CityName
,PositionX
,PositionY
。
我尝试创建一个类:
Public Class CityPosition
Public Shared CityName As String
Public Shared LocX As Double
Public Shared LocY As Double
Public Sub New(ByVal name, ByVal x, ByVal y)
CityName = name
LocX = x
LocY = y
End Sub
End Class
由于我必须收集该类的大量(未知数量)实例,因此我创建了一个ArrayList元素:
Dim CityPositions As New ArrayList
最后,我试图通过以下方法将类的实例添加到ArrayList:
CityPositions.Add(New CityPosition(Positions(0), LocX, LocY))
CityPositions(0).CityName
,但似乎无法正常工作答案 0 :(得分:3)
从公共字段中删除“共享”,如下所示。
Public Class CityPosition
Public CityName As String
Public LocX As Double
Public LocY As Double
Public Sub New(ByVal name As String, ByVal x As Double, ByVal y As Double)
CityName = name
LocX = x
LocY = y
End Sub
End Class
此外,最好在类中使用Properties而不是Public字段。