none
Declare global variable in static class RRS feed

  • Question

  •  

    Hi,

     I have a statuc class in which I'm having the 4 static methods. Now in one method1 I am getting Version information from database and storing it in a local variable lVersion. now I want to use the lVersion value in the method2.

    How to declare the lVersion in Static class.

    Thanks

    Wednesday, August 22, 2007 2:21 PM

Answers

  •  

    IVersion needs to be declared as a global static variable in that case. Example:

     

     

    private static string iVersion = String.Empty;

    ..

    ...

     

    private static void Method1()

    {

       iVersion = "MyValue"; //example we are getting a value from SQL

       Method2()

    }

     

    private static void Method2()

    {

       Console.WriteLine(iVersion); //should show "MyValue";

    }

     

     

    since iVersion is now declared as a global variable for this class, all the methods can see it.

     

    does this help? is this what you are after?

    Wednesday, August 22, 2007 2:28 PM
    Moderator
  • What is stopping you in your goal. That is functional without a problem. Just declare static class and static members and methods. And you can access your static class and it's memebers from everywhere in the project (and all projects that have a reference to that assembly if your project is of class library type).

    public static class Test

    {

        private static string _IVersion;

     

        public static string IVersion

        {

            get { return _IVersion; }

        }

     

        public static void SetVersion(string value)

        {

            _IVersion = value;

        }

    }

    and to alter IVersion you can do this:
    Test.SetVersion("1.0"); //set the value

    string version = Test.IVersion;  //read the value

    as you can see there is no need of creating instance of Test class.

    You can also do this way:

    public static class Test

    {

        public static string IVersion;

    }

     

    Test.IVersion = "2.0";

    string version = Test.IVersion;


    This second example is common way of having global variables behaviour.
    But in C# all objects must be placed in a class, and to acess member in the class without creating instance, member must be static.

    Wednesday, August 22, 2007 4:42 PM

All replies

  •  

    IVersion needs to be declared as a global static variable in that case. Example:

     

     

    private static string iVersion = String.Empty;

    ..

    ...

     

    private static void Method1()

    {

       iVersion = "MyValue"; //example we are getting a value from SQL

       Method2()

    }

     

    private static void Method2()

    {

       Console.WriteLine(iVersion); //should show "MyValue";

    }

     

     

    since iVersion is now declared as a global variable for this class, all the methods can see it.

     

    does this help? is this what you are after?

    Wednesday, August 22, 2007 2:28 PM
    Moderator
  • That's not a global variable (there is no such thing in C#), that's a private static member field.

    Wednesday, August 22, 2007 3:12 PM
    Moderator
  •  

    Nah but us oldies call any variable that can be seen in the entire scope of a class a global one Big Smile
    Wednesday, August 22, 2007 4:09 PM
  • What is stopping you in your goal. That is functional without a problem. Just declare static class and static members and methods. And you can access your static class and it's memebers from everywhere in the project (and all projects that have a reference to that assembly if your project is of class library type).

    public static class Test

    {

        private static string _IVersion;

     

        public static string IVersion

        {

            get { return _IVersion; }

        }

     

        public static void SetVersion(string value)

        {

            _IVersion = value;

        }

    }

    and to alter IVersion you can do this:
    Test.SetVersion("1.0"); //set the value

    string version = Test.IVersion;  //read the value

    as you can see there is no need of creating instance of Test class.

    You can also do this way:

    public static class Test

    {

        public static string IVersion;

    }

     

    Test.IVersion = "2.0";

    string version = Test.IVersion;


    This second example is common way of having global variables behaviour.
    But in C# all objects must be placed in a class, and to acess member in the class without creating instance, member must be static.

    Wednesday, August 22, 2007 4:42 PM
  •  Zamial wrote:

     

    Nah but us oldies call any variable that can be seen in the entire scope of a class a global one
    I'm an oldie too :-) If a variable that can be seen in the entire scope of a class (private static) is a global, then what is a variable that can be seen by all classes? (public static)  Superglobal?  I can see a public static being viewed as a global, but not a private static.
    Wednesday, August 22, 2007 4:45 PM
    Moderator
  •  

    Hmm Big Smile

     

    To be honest Peter i'm probably often using terms out of there correct context but I know what I mean when I say it Wink

    Thursday, August 23, 2007 12:45 PM