barakuda28
barakuda28

Reputation: 2902

Create a class as data structure?

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

Upvotes: 0

Views: 747

Answers (1)

Inisheer
Inisheer

Reputation: 20794

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

Upvotes: 3

Related Questions