Archive for March, 2009
Project Management
I created this sample excel file to track the start and end date for tasks for one particular contract. The challenge I have is that I work 3 days a week on this contract so I needed an easy way to list all my tasks and its duration in days and determine the completion date for this project.
The only downside with this excel file is that all the tasks have to be listed in order. If tasks are reshuffled or a new tasks comes up you have to regroup the tasks manually to bring them in order again.
ReLoad/Refresh an asp.net page
If you mix asp.net and javascript you are bound to run into the following error:

This error is caused when you postback a page and than have some javascript reload the page. The common javascript that causes this error is document.location.reload().
Instead of using the reload command use this instead document.location.href=’url’; it has the same effect as the document.location.reload() except IE does not throw the error since you are reloading the page explicitly instead of having the brower reload the page for you and throwing up the warning.
Having this message come up should raise a red flag that you are mixing asp.net style development (Post-Backs) and client side javascript. Their is not really anything wrong with either development style just don’t mix them.
Factory Class
The factory class is a very common programming concept when you are dealing with polymorphism. However the standard examples are always with an Animal interface and a Cat and Dog object which does explain it properly, however in my 10 years of programming I have never ran into a case where I needed a cat and a dog object. In this blog entry we are going to give an example of polymorphism using a database interface and a SQLServer object and an Access Database object. In plain words we are going to create a database object that can be used for both SQL Server and Microsoft Access.
We start out with a IDatabase Interface
interface IDatabase
{
Connect(string);
ReturnDataTable(string);
Close();
}
public class SQLServer: IDatabase
{
public Connect(string connectionString)
{
}
public DataTable ReturnDataTable(string SQLStatement)
{
}
public void Close()
{
}
}
public class MSAccess: IDatabase
{
public Connect(string connectionString)
{
}
public DataTable ReturnDataTable(string SQLStatement)
{
}
public void Close()
{
}
}
public static class DatabaseFactory
{
public static IDatabase ReturnDatabase(string type)
{
if (type == "SQLServer")
{
return new SQLServer();
}
else if (type == "MSAccess")
{
return new MSAccess();
}
}
}
Sample use of the Database class
IDatabase myDatabase = DatabaseFactory.ReturnDatabase("SQLServer");
myDatabse.Connect("ConnectionString");
DataTabe myData = myDatabase.ReturnDataTable("SELECT * FROM Table");
myDatabase.Close();
C# Callback
Callbacks are usually used by a child class to indicate progress to a parent class. Callback can also used by a child class to call any method in the parent class.
The scenerio I run into a lot during web development is that I have a webpage with a number of web user controls. The save method is implemented at the webpage but I need to call save from one of the web user controls methods.
Instead of using a callback it is very easy for the parent to pass a reference of it self to the child at which point the child can call any public method on the parent their are two problems with this:
- The child can set the parent to null
- It causes a circulare reference.
When implementing a callback:
- The child defines the callback
- The parent attaches a method to the callback
Sample callback example
Parent Class
public class ParentClass
{
private ChildClass MyChildClass = null;
private void SomeMethod()
{
MyChildClass = new ChildClass();
MyChildClass.MyNameCallback += new ChildClass.MyNameDelegate(MyClallBackMethod);
}
private void MyCallBackMethod()
{
}
}
Child Class
public class ChildClass
{
public delegate void MyNameDelegate();
public event MyNameDelegate MyNameCallback;
private void SomeMethod()
{
MyNameCallback();
}
}