|
One of the biggest problems we VB developers migrating
to C# face is understanding STATIC. Static constructors, static data,
static member functions, static properties.
To a VB developer, "static" means a variable
inside a function (method) whose value remains "live" between
function calls, but which is invisible outside the function. In C#, there
is no such concept, because it's not necessary when you can define member
fields instead.
Let's run through the concept of STATIC and explore its
major uses.
In C#, whenever you declare a variable which is bound
to an instance (object) of a class, your variable by default gets
its own copy of all the fields in the class. So for example, if you write:
CreditCardAccountPremium cp1 =
new CreditCardAccountPremium();
CreditCardAccountPremium cp2 = new CreditCardAccountPremium();
cp1.setFirstName("George");
cp2.setFirstName("Harry");
-- the instances cp1 and cp2 each contain their own string
called FirstName. Changing the FirstName in cp2 has no effect on the value
of the FirstName in cp1. However, there are some cases where you do not
want this behavior. Maybe you would like to set a maximum value for a
CashAdvance that is common to the entire CreditCardAccountPremium class.
This would be universally applied to all instances of CreditCardAccountPremium.
We really want the "maxCashAdvance" to be stored
in memory only once, no matter how many instances of the class we instantiate.
To do this, we place the keyword static before the field declaration:
public class CreditCardAccountPremium
{
private static uint maxCashAdvance
= 300;
private string FirstName;
private string LastName;
....
Fields declared with the static keyword are referred
to as static fields or static data., while the other fields like FirstName
are called instance fields. So an instance field belongs to an object,
and a static field belongs to the class as a whole. Static fields exist
from the moment the assembly containing the class is loaded, and are initialized
by the runtime, independent of whether you actually declare any instances
of the class. Consequently, we can also have static constructors,
which serve no other purpose than to assign initial values to static data:
public class CreditCardAccountSuper
{
private static uint maxCashAdvance;
staticCreditCardAccountSuper()
{
maxCashAdvance
= 300;
}
If you were to invoke a CreditCardAccountSuper.maxCashAdvance
property, there is no need to assign an initial value within the Main()
method; the static constructor does this automatically. Note that the
static constructor in which the maxCashAdvance default value is set has
the same name as the class, and cannot take any arguments.
Just as is the case with fields, you can declare methods
as static provided they don't try to access instance data or any other
instance methods. You might want to provide a method to allow users of
the class to view the maxCashAdvance value:
public class CreditCardAccountPremium
{
private static uint maxCashAdvance
= 300;
private string FirstName;
private string LastName;
public static uint GetMaxCashAdvance()
{
return maxCashAdvance;
}
You access static methods and fields associated with
a class differently that you access them as objects (instances of a class).
Instead of specifying the name of the instance to access the method (cp1.GetMaxCashAdvance)
you will specify the name of the class: CreditCardAccountPremium.GetMaxCashAdvance().
You can also access the static members of a class with the "this"
operator: this.maxCashAdvance. The "this" operator is
analogous to the VB "Me" keyword, and is used whenever you want
to make reference to the current object instance. The static modifier
can be used with fields, methods, properties, operators, and constructors,
but cannot be used with indexers, destructors, or types. C#
trashes the whole concept of global functions, variables, and constants.
Instead, you can create static class members, making your C# code not
only easier to read, but less likely to cause naming and other conflicts.
I hope this short discussion helps things tune in better,
because the static is all there for a good reason!
Peter Bromberg is an independent consultant specializing in distributed .NET solutions
in Orlando and a co-developer of the EggheadCafe.com
developer website. He can be reached at pbromberg@yahoo.com
|