Results 1 to 12 of 12

Thread: Global variable

  1. #1

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180

    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

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    public static in your base class .

  3. #3

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180
    So, I declare it in my base class, but I can't give it a size yet.

    Can I redeclare it later?

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    Just use System.Collection.ArrayList instead.... dynamic size increase....
    Magiaus

    If I helped give me some points.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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.

  6. #6

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180
    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

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    In the class NewGame change the declaration of the array to this:

    players = new Player[PlayerCount];

  8. #8

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180
    If I do that, I can't even access the array in the class NewGame anymore.. :dunno:

  9. #9
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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.

  10. #10
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    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.

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 ??

  12. #12
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Arrow 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
  •  



Click Here to Expand Forum to Full Width