June 2007 - Posts

I was working with using the XMLNamespaceManager object the other day, and saw that there was a DefaultNamespace property, but it wasn't assignable.  In doing some research, it turns out that if you want to assign a default namespace, you call AddNamespace with a string.Empty value for the first argument, and the default namespace for the second.  This would be done as such:

objManager.AddNamespace(string.Empty, "http://someuri.com/myschema");

This adds a default namespace to the manager, and provides the value through the DefaultNamespace property.

Posted by bmains | 33 comment(s)
Filed under:

Sometimes some of the objects we create have a lot of code in the constructor.  Being that constructors aren't inherited by a derived class, it may be nice to have this code in a method and run at construction time.  By removing this code out to a method, and changing the approach to the Factory/Factory Method pattern, this would be possible.  Say for the example object:

public class MyClass
{
  public MyClass()
  {
    //Has a lot of code
  }
}

To change this, the logic above can be changed to this:

public class MyClass
{
  private MyClass() { }

  private void InitializeObject()
  {
    //Put code from constructor here
  }
}

To construct this object, the factory method approach could be as below:

public static MyClass CreateMyClassObject()
{
  MyClass cls = new MyClass();
  cls.InitializeObject();
  return cls;
}

So, the construction logic can then be inherited, could be overridden, and all happen at construction time.

Posted by bmains | with no comments
Filed under: