Archive

Archive for the ‘Theory’ Category

Using static properties as class wide data

February 3rd, 2009 fbis No comments

This is one of those things that is so obvious I wonder why it never occured to me before!  If you declare a member as static the compiler only creates one member instance across all class instances.

class ClassWideStatic
{
  protected static int NoOfInstances;
  public ClassWideStatic()
  {
    //setting NoOfInstances here will set it on ALL instances
    //effectively resetting it, but modifying it is fine
    NoOfInstances += 1;
  }
  static ClassWideStatic()
  {
    //This is a special constructor that is only called when
    //the first instance is created
    NoOfInstances = 1;
  }
} 
Categories: Theory, c# Tags:

Classes – Constructors & Chaining

February 1st, 2009 fbis No comments

When we create a class we get given a default blank constructor which is provided by the framework. Its equivalent to

public ClassName() {}

We can create constructors with different parameters:

public ClassName(int One)
{
    this.One = One;
}
public ClassName(int One, string Two)
{
    this.One = One;
    this.Two = Two;
}

Read more…

Categories: Theory, Tutorials, c# Tags: