Make a Variable Available to All Classes?

This is a discussion on Make a Variable Available to All Classes? within the C# Programming forums, part of the General Programming Boards category; I'm relatively new to classes, and I'm trying to figure out something: I have two classes, and they both need ...

  1. #1
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55

    Question Make a Variable Available to All Classes?

    I'm relatively new to classes, and I'm trying to figure out something:

    I have two classes, and they both need to access a boolean array, but if I declare that array in one of the classes, even if it's public, the other class cannot recognize it. How do I work around/fix this?

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109

    Post

    Code:
    public class B{
     public static bool[] ar = new bool[10];
    }
    
    
    public class A{
     //use B.ar[0] ... 
    }
    static is the way to go .

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Grantyt3
    I'm relatively new to classes, and I'm trying to figure out something:

    I have two classes, and they both need to access a boolean array, but if I declare that array in one of the classes, even if it's public, the other class cannot recognize it. How do I work around/fix this?
    Did you instantiate the class? Because, although you can do this by making the array member static, like already mentioned, it probably isn't desired because it'll belong to the whole class? If you dont want this, you could make the arary an instance member and access it with an indexer:

    Code:
        public class Start
        {
            public static void Main( )
            {
                ClassA a = new ClassA( );
                ClassB b = new ClassB( );
    
                a[3] = true;
                b.Boolean = a[3];
                Console.WriteLine(b.Boolean); // prints True
            }
        }
    
        public class ClassA
        {
            private bool[] values = new bool[5];
    
            public bool this[int index]
            {
                get { return values[index]; }
                set { values[index] = value; }
            }
        }
    
        public class ClassB
        {
            private bool _boolean = false;
    
            public bool Boolean
            {
                get { return _boolean; }
                set { _boolean = value; }
            }
        }
    }

  4. #4
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Yep, I got it working, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes: adding a new variable produces errors
    By drunkuilled in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 12:58 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  3. which variable can store words?
    By Hunterofman in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2008, 06:59 PM
  4. Abstract classes
    By cisokay in forum C++ Programming
    Replies: 17
    Last Post: 05-29-2005, 10:39 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21