OOP question (global variable?)

This is a discussion on OOP question (global variable?) within the C# Programming forums, part of the General Programming Boards category; Hi everyone, I have OOP question specific to C#. I'm fairly new to programming so bare with me. I have ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    OOP question (global variable?)

    Hi everyone, I have OOP question specific to C#.

    I'm fairly new to programming so bare with me.

    I have 3 classes.
    Code:
    Class 1
    {
    
    }
    Class 2
    {
    
    }
    Class 3
    {
    
    }
    Now, I need to create a variable that i can read and write from any of those 3 classes.
    How can I do this?

    Thank you for your time!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1) It's "bear", not "bare". I won't get naked with you.

    2) There are various ways of sharing data. Which one is appropriate depends on the situation. You could have a member of one of those classes that provides a getter and a setter, so that all code that has access to an instance can modify the member. You could have a static member of one of the classes. You could have a fourth, singleton class that provides the shared data.
    What exactly is the purpose of this variable you need to share?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Thanks for clearing up the bear-bare misunderstanding.
    I Google'd it for accuracy, and by golly, you are right. Quite incompetent of me.

    Also thanks for being the only person to reply.

    I need to create a variable that my program can set to true or false (bool), so that all the other classes can check the value of it.
    Code:
    Class1
    {
    if (variablename == false)
    break;
    }
    Class2
    {
    if (variablename == false)
    break;
    }
    Class3
    {
    if (variablename == false)
    break;
    }
    Can you show me example of how to make this 'variablename' accessible by all 3 classes?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could have a base class for all your classes with a static member variable. Having it "Protected" also prevents other classes from touching it, only the ones that inherit from Base can use it. Depending on the situation this may or may not be the best/finest solution.
    Code:
    class Base
    {
      protected static bool Flag = false;
    }
    
    class Child1 : Base
    {
      public void DoStuff()
      {
        if(Flag) return;
      }
    }
    
    class Child2 : Base
    {
      public void DoMoreStuff()
      {
        if(Flag) return;
      }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It still depends on what the variable represents.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    A simple way is as CornedBee said. Have one of the classes own the private storage or what Microsoft calls the backing storage of the variable. Create a public property method that will get/set the variable.

    Code:
    public class 1
    {
      private bool m_Value;
    
      public Value
      {
         get
         {
            return m_Value;
         }
         set
         {
            m_Value = value;
         }
      }
    
    };
    
    public class 2
    {
    };
    
    public class 3
    {
    };

    As long as 2 and 3 can access Value in 1, everything will work fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global variable
    By 182 in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2006, 09:33 PM
  2. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 08:37 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM
  4. Global variable question
    By csisz3r in forum C Programming
    Replies: 10
    Last Post: 09-19-2005, 08:19 AM
  5. Class member using a global variable, causing problems.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2005, 12:27 AM

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