CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2002
    Location
    Germany
    Posts
    487

    How can init a class containing an array?

    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

  2. #2
    Join Date
    Nov 2008
    Posts
    1

    Re: How can init a class containing an array?

    Code:
    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...
    Last edited by DDavich; November 20th, 2008 at 08:16 PM.

  3. #3
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: How can init a class containing an array?

    nice solution.
    May I suggest something safer - use the new sub:
    Code:
    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
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  4. #4
    Join Date
    May 2002
    Location
    Germany
    Posts
    487

    Re: How can init a class containing an array?

    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

  5. #5
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: How can init a class containing an array?

    You can do that:
    Code:
    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.
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured