|
-
Mar 1st, 2004, 12:40 PM
#1
Thread Starter
Banned
Global variable
Hi,
I have a bunch of classes.
One of them is called Player.
Now in another class I want to create an array of Players. That's no problem.
But I wonder where I should declare this array so I can use it throughout my application. Because (obvious) if I declare the array in the class NewGame, I can't use it in the class Game or something..
How can I make the array 'global'..
Thanks,
MK
-
Mar 1st, 2004, 04:43 PM
#2
Sleep mode
public static in your base class .
-
Mar 1st, 2004, 04:59 PM
#3
Thread Starter
Banned
So, I declare it in my base class, but I can't give it a size yet.
Can I redeclare it later?
-
Mar 1st, 2004, 10:14 PM
#4
Frenzied Member
Just use System.Collection.ArrayList instead.... dynamic size increase....
Magiaus
If I helped give me some points.
-
Mar 2nd, 2004, 05:00 AM
#5
Sleep mode
Originally posted by Michael_Kamen
So, I declare it in my base class, but I can't give it a size yet.
Can I redeclare it later?
You can do something like this :
string[] str=null;
and when you need to use it do this ;
str=new string[5] .
I would go with Magiaus and say use ArrayList.
-
Mar 2nd, 2004, 05:18 AM
#6
Thread Starter
Banned
Well, it doesn't seem to work.
I have this in my base class:
Code:
public static Player[] players;
Then in the class NewGame I have this:
Code:
Player[] players = new Player[PlayerCount];
Then I assign some values to the properties of the players, like players[i].Name etc..
Then when I try to access them in the class Game(), it doesn't work. I can't get the players[i].Name anymore for instance...
What am I doing wrong? :dunno:
I can't access the
-
Mar 2nd, 2004, 05:29 AM
#7
Sleep mode
In the class NewGame change the declaration of the array to this:
players = new Player[PlayerCount];
-
Mar 2nd, 2004, 06:25 AM
#8
Thread Starter
Banned
If I do that, I can't even access the array in the class NewGame anymore.. :dunno:
-
Mar 2nd, 2004, 08:39 AM
#9
Hyperactive Member
To make what you want to do work do something like this:
PHP Code:
using System;
namespace Games
{
public class MainApp
{
public static void Main(string[] args)
{
Checkers checkers = new Checkers(3);
Console.WriteLine("Total Players: {0}", GameBase.Players.Length);
Console.ReadLine();
}
}
public class GameBase
{
public static Player[] Players = null;
}
public class Checkers : GameBase
{
public Checkers(int playerCount)
{
GameBase.Players = new Player[playerCount];
}
}
public class Player
{
}
}
Since you're making that Players array static in the base class your subclass won't pick that up, you have to call it directly on the base class.
-
Mar 2nd, 2004, 07:39 PM
#10
Frenzied Member
ArrayList
Code:
public class Player{}
public class PlayerCollection : System.Collections.ICollection
{
System.Collections.ArrayList players = new System.Collections.ArrayList();
public int Add(Player player)
{
return players.Add(player);
}
public void Remove(Player player)
{
players.Remove(player);
}
#region Implementation of ICollection
public void CopyTo(System.Array array, int index)
{
players.CopyTo(array, index);
}
public bool IsSynchronized
{
get
{
return players.IsSynchronized;
}
}
public int Count
{
get
{
return players.Count;
}
}
public object SyncRoot
{
get
{
return players.SyncRoot;
}
}
#endregion
#region Implementation of IEnumerable
public System.Collections.IEnumerator GetEnumerator()
{
return players.GetEnumerator;
}
#endregion
public Player this[int index]
{
get
{
return players[index];
}
set
{
players[index] = value;
}
}
}
public class Games
{
private PlayerCollrction players = new PlayerCollection();
public PlayerCollection Players
{
get{return players;}
set{players = value;}
}
}
Magiaus
If I helped give me some points.
-
Mar 2nd, 2004, 09:57 PM
#11
Sleep mode
Originally posted by Magiaus
Code:
public class Player{}
public class PlayerCollection : System.Collections.ICollection
{
System.Collections.ArrayList players = new System.Collections.ArrayList();
public int Add(Player player)
{
return players.Add(player);
}
Shouldn't this be a void and returning null ??
-
Mar 3rd, 2004, 02:07 AM
#12
Frenzied Member
it could be
but it returns the index of the player
Magiaus
If I helped give me some points.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|