Adivce on Method overloads.
Posted
by Muhammad Kashif Nadeem
on Stack Overflow
See other posts from Stack Overflow
or by Muhammad Kashif Nadeem
Published on 2010-03-25T11:47:41Z
Indexed on
2010/03/25
12:03 UTC
Read the original article
Hit count: 404
c#
Please see following methods.
public static ProductsCollection GetDummyData(int? customerId, int? supplierId)
{
try
{
if (customerId != null && customerId > 0)
{
Filter.Add(Customres.CustomerId == customerId);
}
if (supplierId != null && supplierId > 0)
{
Filter.Add(Suppliers.SupplierId == supplierId);
}
ProductsCollection products = new ProductsCollection();
products.FetchData(Filter);
return products;
}
catch
{
throw;
}
}
public static ProductsCollection GetDummyData(int? customerId)
{
return ProductsCollection GetDummyData(customerId, (int?)null);
}
public static ProductsCollection GetDummyData()
{
return ProductsCollection GetDummyData((int?)null);
}
1- Please advice how can I make overloads for both CustomerId and SupplierId because only one overload can be created with GetDummyData(int? ). Should I add another argument to mention that first argument is CustomerId or SupplierId for example GetDummyData(int?, string). OR should I use enum as 2nd argument and mention that first argument is CustoemId or SupplierId.
2- Is this condition is correct or just checking > 0 is sufficient -> if (customerId != null && customerId > 0)
3- Using Try/catch like this is correct?
4- Passing (int?)null is correct or any other better approach.
Edit:
I have found some other posts like this and because I have no knowledge of Generics that is why I am facing this problem. Am I right? Following is the post.
http://stackoverflow.com/questions/422625/overloaded-method-calling-overloaded-method
© Stack Overflow or respective owner