-----------------------------------------------------------
New Message on MumbaiUserGroup
-----------------------------------------------------------
From: AjaySingala[Synergetics]
Message 5 in Discussion
Treez,
Let's say for e.g.; you have a class "Customer" as follows:
public class Customer
{
private int mCustId;
private string mFName;
//
// The rest of the members.
//
public int CustomerId
{
get { return mCustId; }
}
public string FirstName
{
get { return mFName; }
set { mFName = value; }
}
//
// The rest of the member properties.
//
public void Customer()
{
}
public void Customer(int id)
{
mCustId = id;
//
// Code to fetch data from database
// and update member of the Customer class.
//
}
}
Now here, there are 2 constructors for the class. One I use to create an empty
Customer object and the other when I know the "id" of the customer and want a filled
in object. Here's where overloading is useful.
Another e.g.; Take the same "Customer" class which has this method:
public Customer GetCustomer()
{
//
// Call Stored Procedure passing "mCustId" as parameter
// and fetch customer data.
//
}
Now, what if I need to fetch the customer data based on "First Name"? Then, instead of
writing a method like "GetCustomerByFirstName(string name)", I can simply write one
more GetCustomer method as follows:
public Customer GetCustomer(string fname)
{
//
// Call Stored Procedure passing "fname" as parameter
// and fetch customer data.
//
}
Similarly, you could have another "GetCustomer" method to search on something else.
Here, the advantage is that I need not remember what method to call for specific
search criteria. What if I don't name the methods properly. You can't just rely on
the"method name" to determine what it will do, although, it's always better to give
appropriate method names, but that may not necessarily be the case.
But overloading has it's rules too as follows:
1) You cannot overload a method based on it's return value. For e.g.; this is wrong:
public Customer GetCustomer(string fname)
{
//
// Call Stored Procedure passing "fname" as parameter
// and fetch customer data.
//
}
public Customer GetCustomer(string city)
{
//
// Call Stored Procedure passing "city" as parameter
// and fetch customer data.
//
}
The compiler will not accept it. The parameter types and the number of parameters have
to be different for overloading a method.
2) Overloading cannot be done on "return type" of method. For e.g.; this is wrong:
public Address GetCustomerAddress(int id)
{
}
public string GetCustomerAddress(int id)
{
}
Here, the first method returns an "Address" object while the second returns a string.
So the only difference between the two methods is their return types. Well, the
compiler throws an error in this case.
3) You cannot overload based on parameter names". For e.g.; this is wrong:
public Address GetCustomerAddress(int id, bool bCity)
{
}
public string GetCustomerAddress(int custid, bool bCityRequired)
{
}
Just changing the "names" of the parameters, without changing their "order" will not
work. This will work:
public Address GetCustomerAddress(int id, bool bCity)
{
}
public string GetCustomerAddress(bool bCityRequired, int custid)
{
}
HTH.
Regards,
Ajay Singala [Synergetics].
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/mumbaiusergroup/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]