0

Im making a sim of a robocup for a project and it would really help make the code easier to read if I could have an array of players but I cant dim and array as new and my players are all a class I made called gameobject.
How do I make an array of players as my class gameobject?

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Chris11246
Featured Replies
  • you need to initialize each player as new gameobject [CODE=vb] Dim players(9) As gameobject For i As Integer = 0 To players.Length - 1 players(i) = New gameobject players(i).testProperty = CStr(i) Next For i As Integer = 0 To players.Length - 1 Console.WriteLine(players(i).testProperty) Next [/CODE] Read More

0
Dim i As Integer = -1
        i += 1
        ReDim Preserve MyPlayers(i)
        MyPlayers(i) = TextBox1.Text

Edited by Nick Evan: Added code-tags

0

thats not the problem I wanted to know if there was a way to dim an array as a class like:
Dim players(9) as new gameobject
but you cant dim one as new and if i just do:
Dim players(9) as new gameobject
it says "object reference not set to an instance of an object"

1

you need to initialize each player as new gameobject

Dim players(9) As gameobject
        For i As Integer = 0 To players.Length - 1
            players(i) = New gameobject
            players(i).testProperty = CStr(i)
        Next

        For i As Integer = 0 To players.Length - 1
            Console.WriteLine(players(i).testProperty)
        Next
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.