Tuesday, September 4, 2007

why are interfaces used?

Interfaces in OOPs are very powerful when it is used wisely. One of the best uses of an interface is to decouple the classes or reduces the dependency between two classes. The following example illustrates a scenario where interfaces can be used to decouple two classes, management and employee type classes.

Let us take an example of a company which has a managment entity and other workers like clerks and engineers. Assume that the management wants to find out the manager of these two worker entities from the OOPs point of view.

Bad example.

A naive programmer would implement something like this

class management
{

public string manager(clerk clerkObject)
{

return clerkobject.manager();
}

public string manager(engineer engineerObject)
{

return engineerObject.manager();
}


}


class clerk
{

public string manager()
{
return "Pradeep";
}

}


class engineer
{

public string manager()
{
return "Pramod";
}
}

The above code will return the name of the manager heading the clerk or engineer class of employees. Even though the code returns the correct result, the design is not good from OOPs point of view. A better way would be the following


class management
{

public string manager(IManager managerObj)
{

return managerObj.manager();
}

}


interface IManager
{
string manager();
}


class clerk:IManager
{

public string manager()
{
return "Pradeep";
}

}


class engineer:IManager
{

public string manager()
{
return "Pramod";
}
}

In the above example we have decoupled the managemened class from the Clerk and engineer class, at the same time getting the result that we want.

Sunday, September 2, 2007

Thin line in HTML using CSS

I really like the power of CSS. We often find some visual elements on the HTML pages, that may look simpler, but we cannot create them ourselves with the basic HTML elements.

One of these visual object is a very thin line which is not possible through the simple HR tag the HTML. We need to use the power of CSS in order to draw a thin line. Following is how we can do this.

Just define the following style sheet element between the head element in the HTML page

<style type="text/css">

hr{height:1px}

</style>

After you have defined this, everytime you use a


tag, a thin line will be displayed instead of a the default think line.

CSS is really powerful if used properly.

Accessing Parent page objects from User Control

Sometimes a user control needs to access some public methods or variables or any other objects for that matter in its parent page. The user control provides the Page object which gives access to its container parent. The problem with this page object is that, it is the general page object. This means that this page object doesnt contain the necessary type details which will enable you to access the object in the required parent container.

In order to achieve this, the general page object has to be type casted to its parent type after which the accessible objects within the parent will be visible to the intellisense.

For eg. if the parent page has the following code..


namespace testwebapp
{
public partial class parentpage : System.Web.UI.Page
{
public string setLabelvalue
{
set
{
Label1.Text = value;
}
get
{
return Label1.Text;
}
}
}
}


In the above code, I am seeting the value of the label to string 'Pradeep'. In order to call this public property from the user control contained in this page, use the following code.



namespace testwebapp
{
public partial class ChildControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
((parentpage)Page).setLabelvalue = "pradeep";
}
}
}


In the above code the following statment type casts the Page object to the 'parentpage' type.

((parentpage)Page).setLabelvalue = "pradeep";