*
A Method that does not have a return type.
E.g.
public class in4DataGrid
{
//Constructor declaration. Note after the modifier "public" keyword,
there is no //mention of a datatype or void. Also the name is same
as that of class.
public in4DataGrid()
{
// Some Code Here.
}
}
* Can either be static or Instance based.
E.g.
public class in4DataGrid
{
//Static Constructor. Modifiers are irrelevant here.
static in4DataGrid()
{
// Some Code Here.
}
//Instance based
public in4DataGrid()
{
// Some Code Here.
}
}
* Take parameters and behave just like any other method, which means
that any instructions can be encapsulated in a constructor.
E.g.
public class in4DataGrid
{
private int intWidth;
private int intHeight;
//Parameters intWidth and intHeight of type int.
public in4DataGrid(int Width, int Height)
{
intWidth = Width;
intHeight = Height;
//Some Code Here.
}
}
* Constructors can be declared "private or protected" even though
declaring them public is a common scenario. When declared as private,
one cannot create an instance of the class or derive the class.
* Can be overloaded. Further, one overloaded constructor can call
the other one to take advantage of encapsulation and take negate the
risks of code duplication within overloaded constructors.(See the
following section examples)
The following section deals with the how best to leverage the features
of Constructors in our classes.
How to Use?
* Use application logic in constructors carefully. Try to minimize
unnecessary database operations in constructors like loading a Dataset
that is used only in specific sections or on demand from the calling
class. This is a performance overhead.
* Do not write redundant code in overloaded constructors. This will
become a huge maintenance issue as your class hierarchy grows and
you start implementing more overloaded constructors in your derived
classes. The following example shows a common section where in validation
of initialization logic happens.
E.g.
public class in4DataGrid
{
private int intWidth;
private int intHeight;
//Overloaded constructor 1 with no parameters. But the constructor
initializes //the values by calling the other constructor which holds
common business //validations by using the keyword "this".
public in4DataGrid():this(200,300)
{
//Some Code Here.
}
//Overloaded constructor 2 with parameters intWidth and intHeight
of type //int.
public in4DataGrid(int Width, int Height)
{
intWidth = Width;
intHeight = Height;
// Do some business validation here.
if(intWidth > intHeight)
{
//Some code here
}
}
}
* Try to use static constructors and variables when you are dealing
with Global variables. This is especially a good design practice since
you can initialize the static variables easily within static constructors
instead of using constant variables. Other advantage would be to encapsulate
your instruction logic for these variables.
E.g.
public class in4DataGrid
{
private int intWidth;
private int intHeight;
private static int intMinimumHeight;
//Overloaded static constructor 1 with no parameters. But the constructor
initializes the values by calling the other constructor.
static in4DataGrid()
{
//Some Business logic Here.
//Initialize the value based on logic.
intMinimumHeight=100;
}
//Overloaded instance based constructor 2 with parameters intWidth
and //intHeight of type int.
public in4DataGrid(int Width, int Height)
{
if(Width > intMinimumWidth)
{
intWidth = Width;
intHeight = Height;
}
else
{
//Some Code Here.
}
}
}
* Overloaded static constructors are dangerous when used to call other
constructors, so try not to call other static constructor from within.
* Design your derived class constructors carefully. This is because
when an instance of derived class is created there is more than one
constructor that will be called, i.e. both the base class and the
derived class in the hierarchy. The constructors of the derived class
first will run constructors of base class. The issue arises when you
have overloaded constructor definitions in the base and derived classes.
Overlooking here would prove costly. Make sure you use the keyword
"base()" to make calls to overloaded base classes.
E.g.
public class in4DataGrid: System.Data.DataGrid
{
//Constructor declaration. Use of keyword "base()" makes sure that
the call is made to the base class(here it is System.Data.DataGrid).
One can call any overloaded constructor of the base class this way.
public in4DataGrid(): base()
{
// Some Code Here.
}
}
Also note that the constructor initializers "base" and "this" are
the only keywords that can be used when making a call to the base
constructor, also any one among these can be specified.
Conclusion
Proper design of constructors goes a long way in solving some core
problems faced in your class design. C# supports enormous flexibility
to accommodate the use of various types of constructors. One has to
judge the need of the application and never overlook the importance
of constructors.
About the Author:
Vishnu Prasad H is an Microsoft MVP and working as Consultant > (Technical),
Capco IT Serives India-Bangalore. Expertise in MS based > technologies:
VB6, ASP, COM+, SQL 2K, C#,XML, ADO. Net, ASP. Net. > Contact: Vishnuprasadh@yahoo.com
Read this Newsletter at: http://www.cprogrammingtrends.com/2003/1230.html |
|
| From
the Forum: |
| Logging Users |
I have a website TheDigitalHorse.com where I sell memberships and allow members to allow copyrighted information that I have illustrated with digital animations (FLASH)
My frontend is a ASP script where users sign up and pay with a credit card then get a username(email) and password that they can log on with.
I would like to be able to track who is logged in and track usage. I am trying to create a measure to see if I have duplicate users or people sharing user information. I would like to create a log file and another file where I can see if the same user is logged in at the same time. ...
|
|