0

I am trying to create a data structure CityPosition, in which to have 3 variables: CityName, PositionX, PositionY.

I tried creating a class:

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

As I have to collect lots (unknown number) of instances of that class, I created an ArrayList element:

Dim CityPositions As New ArrayList

Finally, I am trying to add an instance of the class to the ArrayList by this:

CityPositions.Add(New CityPosition(Positions(0), LocX, LocY))
  1. Please let me know if I am doing it right, because I am a VB.NET newbie.
  2. How to access the instance properties? I tried CityPositions(0).CityName but it seems not to work
4

1 回答 1

3

Remove "Shared" from your public fields as shown below.

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

Also, it is preferable to use Properties instead of Public fields within a class.

http://msdn.microsoft.com/en-us/library/dd293589.aspx

于 2012-10-27T17:11:31.113 回答