Archive

Archive for February, 2009

Create controls dynamically from database

February 11th, 2009 fbis No comments

In my cms’ I usually have the option of creating controls from the database. This is just a simple use for reflection but allows me to add controls to page just by adding a new row.  If its one of my controls it supports a persistance system that allows the control to save and restore itself easily.

Read more…

Categories: .net, c# Tags:

Rendering Paths as a nested List

February 4th, 2009 fbis No comments

Keeping tree data in a database can be a nightmare. There are a number of techniques to achieve it but they are all either unwieldy on the sql side (multiple joins) or on the data side (binary trees). I needed an easy way to render a tree out in a single read from the db.
I achieved this by just having a path field in the table, ordering by it and then dumping it out with the following routine. Needs more work but ok for the mo :) Would need to change to writer instead of StringBuilder and I need to sort out the items that appear at the top (would appear under the folders in a treeview) Read more…

Categories: c# Tags:

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: