1

Is there really any good situation where it would be nice to have all objects of a class share a variable? Besides constants, of course.

Angelo Rivera
  • 121
  • 1
  • 1
  • 11

3 Answers3

0

there is no one answer for that. there is all sort of reasons to use and not to use. for example, a class counter the checks how many instances from this class created could be a static variable in a class. that really depends on what you are programing and why.

if you'll look here on the first answer there is also some answer for you

Community
  • 1
  • 1
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

Static variables can be used to share a variable across all instances of a class, like in this example:

    class Program
    {
        static void Main(string[] args)
        {
            //Create new instances of the Employee class
            Employee joe = new Employee("joe", 1);
            Employee billy = new Employee("billy", 2);

            Console.WriteLine(Employee.EmployeeCount);
            Console.ReadKey();
        }

    }

    class Employee
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public static int EmployeeCount { get; set; }

        public Employee(string n, int id)
        {
            this.ID = id;
            this.Name = n;
            EmployeeCount++;
        }
    }

As opposed to static variables, instance variables allow each Employee class to have their own Name and ID variables.

pcnThird
  • 2,362
  • 2
  • 19
  • 33
0

The answer is Yes, there are situations you would like to use a static variable and have all instances of a class share this variable.

Example

Lets say you have a book collection. You can keep track of number of books by adding a static field of bookCount and increasing it's value when a book is created.
You can take a look at @pcnThird answer for a code example.

My favorite usage for static variables

I find static fields most effective when I want to use a field that should behave as a constant field, but I can not know it's value at compile time.
for example lets say you have a log class. you may create many instances of this log class and every instance will log to a different location on your file system.
If you want all of your logs files to have the same prefix including the date, it can not be a constant as the date has an annoying habit of changing... The solution could be a static field (I usually prefer a static readonly which acts as a constants after the constructor has completed it's job).

Public class Log
{
    public static readonly string LogFilenamePrefix;
    Public Static Log()
    {
        Log.LogFilenamePrefix = DateTime.Today.ToString()+"_MyLogFile";
    }

}

In this example you can also see that the DateTime class uses as Static Today variable, which brings me to the next example:

The obvious example

There is of course the situation of static classes where you don't have a choice but using static variables...

P.S. Some consider static variables to be evil. Take a look at this SO post.

Community
  • 1
  • 1
Avi Turner
  • 10,234
  • 7
  • 48
  • 75