Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Identifying static variables in a reuseable component

3.67/5 (2 votes)
17 May 2010CPOL2 min read 10K  
In a class, when we preceed a class member (variables/methods) with the keyword static in C# or shared in VB.Net then the 2 things gets added to the behavior of the variable:-The class member could be accessed without creating instance of the class. The class member will shared across...
In a class, when we preceed a class member (variables/methods) with the keyword "static" in C# or "shared" in VB.Net then the 2 things gets added to the behavior of the variable:-

The class member could be accessed without creating instance of the class. The class member will shared across all instances of that class.
In case the class member is a method, a restriction is imposed on to it, i.e. it can access only static or local variables.
Thus if we declare a class as below:-

public class Employee
{

private string _sEmpName;

public string EmpName
{
get{return _sEmpName;}
set{_sEmpName = value;}
}

private int_iEmpAge;

public int EmpAge
{
get{return _iEmpAge;}
set{_iEmpAge = value;}
}

public static string _sLocation;

}

Now if we want to use 2 objects of this Employee class as below:-

Employee objEmp1 =new Employee();
objEmp1.EmpName = "Riya";
objEmp1.EmpAge = 29;
Employee.sLocation = "New Delhi";

Employee objEmp2 =new Employee();
objEmp2.EmpName = "Nikhil";
objEmp1.EmpAge = 32;
Employee.sLocation = "Mumbai";

Thus in the example above for the objEmp1 the sLocation is set to "New Delhi" then again for objEmp2 the sLocation is set to "Mumbai". This completely overwrites the value of that had been wrriiten for objEmp1 since sLocation has a single instance that is shared across all objects of the type Employee.

Thus when we are creating a class the list given below may act as a set of guidelines for identifying static and non-static members:-

* Any member that helps in unique identification of a particular instance of the class should be a non-static member.
* Any member that acts as properties for particular instance of the class should be non-static.
* Any method that manipulates non static members should be non-static.
* Any member whose value is calculated one time and is same across all objects of the class should be a static member.
* Any member whose value is set by a pre-defined business logic and acts as readonly property for all objects can be marked as static.
* Any method that manipulates static members should be marked as static.
* Any method that does not access any class members and returns void can be marked as static.
* Any method that does not access any class member and does not return an object of that class should be marked as static.

Thus when we are creating a class, in order to identify the static and non-static members we need answer the following questions: -

* What does the member do?
* What is the scope of the member?
* Who can access the values of the member?
* Who can manipulate the values of the members?


Only if the above answers are correctly answered for each member of a class that we wish to be reusable in form of user/custom controls then only can we expect the user control to behave the way we want to.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)