.comment-link {margin-left:.6em;}
Books & Articles I wrote.

Friday, February 24, 2006

 

Setting Properties Properly

Always set publically accessible (and protected and many private) values through properties. Never do it directly. Even is there is no logic at the moment, there may be in the future and setting them directly (say through private properties) will prevent using this logic in the future.

If you need to set a public property as readonly, but still set it in a non-public manner, then you can just a get only on the property.

Rather than setting the global variable the prooerty refers to directly, use a protected or private SetProperty() method which can contain any required logic.

So, say i have the property EmployeeID:

int _employeeID;

public EmployeeID
{
get
{
return _employeeID;
}
}

In the code this should be set as follows:

protected SetEmployeeID(int employeeID)
{
_employeeID = employeeID;
}

This page is powered by Blogger. Isn't yours?

Weblog Commenting and Trackback by HaloScan.com