Click to See Complete Forum and Search --> : How can init a class containing an array?


Marc from D
November 20th, 2008, 03:57 PM
Hello!

please help me with my beginners-question:

I try to program a Handball-timer-program. Just for fun, as I want to learn some .net programming using basic.

I want to define my data structures first:
so I created a "player" class, containing string for name and a byte for yellow and red cards.
Also I created a "team"-class, containing a single "player".
The "game"-class contains two "team"-s.

1. question: how can I tell my "team"-class to contain an array of 16 elements of "player"

2. how can I init all fields so they do not contain "NULL" but a valid value, i. e. an empty string as the players name?


Thanks a lot!

Marc

DDavich
November 20th, 2008, 07:12 PM
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Game As New Game
Dim Player As New Player
For SixteenPlayers As Integer = 1 To 16
With Game
.Baseball.BlueJays.Add(Player)
.Basketball.Raptors.Add(Player)
End With
Next
End Sub 'Break it here and you can see the "Game" object nicely

Public Class Game
Public Basketball As New Basketball
Public Baseball As New Baseball
End Class

Public Class Basketball
Public Raptors As New ArrayList
End Class

Public Class Baseball
Public BlueJays As New ArrayList
End Class

Public Class Player
Public PlayerName As String = ""
Public PlayerNumber As String
Public PlayerSucks As Boolean
End Class

End Class

Here's an example of what I think you might be looking for. As you can see the Game object can grow very quickly and it's very easy to work with for changes etc...

javajawa
November 21st, 2008, 06:33 AM
nice solution.
May I suggest something safer - use the new sub:
Public Class Player
End Class

Public Class Team
Private mPlayers() As Player
Private mTeamName As String

Public Sub New(ByVal TeamName As String, ByVal Players As Integer)
mTeamName = TeamName
mPlayers = Array.CreateInstance(GetType(Player), Players) 'This auto-magically create the array structure for a 1-d array of n items of type t...
For I As Integer = 0 To Players - 1
mPlayers(I) = New Player()
Next
End Sub

Public ReadOnly Property TeamName() As String
Get
Return mTeamName
End Get
End Property

Public Property Player(ByVal Index As Integer) As Player
Get
Return mPlayers(Index)
End Get
Set(ByVal value As Player)
mPlayers(Index) = value
End Set
End Property
End Class

Marc from D
November 21st, 2008, 08:02 AM
Thanks a lot! I will try both ways later, when it is late evening here in germany.

But please -if possible- give me a hint why it is forbidden to do something like this in a class:

class player
dim x as string
...
end class

class team
dim Teammember(16) as player
...
end class

I think, that the compiler knows everything it takes to create the machine code. But it is not willing to initialize an array with a given size.What is the advantage of an arraylist compared to a plain array?

You see, I'm just wondering.

Again: Thanks alot for the already given code and hints.


Marc

javajawa
November 21st, 2008, 11:40 AM
You can do that:

Public Class Test
Dim A(16) as Whatever
Dim X as String
End Class
It works just fine here.
But defining an array only reserves the space in memory - it doesn't initialise the variables if they are user defined. This has to be done manually, either by using "New Whatever()" on each element, or using the array's Initialise sub routine.