I have a class called Country. I can instantiate it by Country myCountry = new County();
It has method, AddTown(string townName) { ... } which can be called like myCountry.AddTown("London");
How can I add an alternative method with identical functionality but which is called like myCountry.Towns.Add("London");
I cant figure out how to implement the '.Towns.' part of the naming structure.
VulpesPosted Feb 13, 2014, 6:57 AM
So Customers is a DataTable in the Northwind DataSet.
I wouldn't bother trying to mimic this unless your Country class contains a lot of sub-collections such as Towns, Provinces, Rivers, Mountains etc.
Eric BroughPosted Feb 13, 2014, 6:22 AM
I have a feeling that a Collection may be involved here - something I've not tackled before.
Jignesh TrivediPosted Feb 13, 2014, 2:56 AM
hi,
are you looking extension method kind of solution?
like string has inbuilt extension method called ToUpper(), ToLower() etc
VulpesPosted Feb 12, 2014, 2:01 PM
class Country
{
private List
public Country()
{
_towns = new List
}
public void AddTown(string town)
{
_towns.Add(town);
}
}
If that's the case, then you could implement the additional functionality as follows:
class Country
{
private List
public List
public Country()
{
_towns = new List
}
public void AddTown(string town)
{
_towns.Add(town);
}
}
You can then do:
Country myCountry = new Country();
myCountry.Towns.Add("London");