Application Global Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aads
    New Member
    • Mar 2008
    • 48

    Application Global Variable

    Hi all,

    Is there any way where in I can declare a variable & access it in any of the projects of my solution?

    For example:

    I have a solution under which I have 2 projects namely projectA & projectB.
    Now I want to declare a variable globally so that I can access the same variable in both projects projectA & projectB.

    Or

    Is there any other alternate solution for this?

    Any help would be appreciated.

    Thanks,
    Aads
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    Hi there,

    Global variable should be avoided when possible. My personal approach to these situations is to create a common project. In this project i define classes and have all other projects reference this project (via add reference menu option)

    You can now create an instance of that class in any project which references the common project. I then create methods to perform the required functionality. by adding the ref keyword in front of variable declerations the function is allowed to change the actual value of the variable.

    Normally i have an interface to my class which exposes some properties. One class may then set a property while another may get this property at a later stage.

    Hope this helps

    Comment

    • Aads
      New Member
      • Mar 2008
      • 48

      #3
      Thanks for the quick reply.

      I totally agree with you that using global variables is always an additional overhead. But as you mentioned that an instance of any class belonging to a common project can be created by any of the classes in any of the projects, that's fine. But supposing if a class in projectA created an instance of a class in projectCommon, is that instance available to any class of any other project?

      i.e. projectA's some class created an instance of projectCommon's some class.
      Now I want to access the same instance (created by projectA's some class) in some other project say for example projectC.


      Thanks,
      Akash

      Comment

      • cloud255
        Recognized Expert Contributor
        • Jun 2008
        • 427

        #4
        You need to use static properties and acces the class via typed names and not an instance, i will try to clarify:

        the common class (defined in the common project):

        Code:
        public class MyClass
            {
                public MyClass()
                {
        
                }
        
                private static int _SomeValue;
        
                public static int SomeValue
                {
                    get
                    {
                        return _SomeValue;
                    }
                    set
                    {
                        _SomeValue = value;
                    }
                }
        }
        Now to set the value, in class 1 (project A):

        Code:
        WindowsFormsApplication3.MyClass.SomeValue = 12;
        And then to access the property in class 2 (project B):
        Code:
        MessageBox.Show(WindowsFormsApplication3.MyClass.SomeValue.ToString());
        This code is not the most neat and uses some fairly bad names, but i didn't know how to explain this without an example. With the same approach you can access members and methods too. Just remember to make things static and access via typed name not instance i.e. going :
        Code:
        WindowsFormsApplication3.MyClass instance = new WindowsFormsApplication3.MyClass();
        will not allow you to 'share data' across projects.

        Hope this helps.

        Comment

        • Aads
          New Member
          • Mar 2008
          • 48

          #5
          Excellent example! But the point to be noted in the above example is that all the variables/properties/functions will have to be declared as static.

          Thanks a lot.

          Comment

          • cloud255
            Recognized Expert Contributor
            • Jun 2008
            • 427

            #6
            Originally posted by Aads
            Excellent example! But the point to be noted in the above example is that all the variables/properties/functions will have to be declared as static.

            Thanks a lot.

            you're welcome :) My apologies for not being more clear...

            Comment

            Working...