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.

VulpesRank #55 · 29.2K reputationPosted Feb 12, 2014, 9:01 AM EST
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");
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 12, 2014, 9:56 PM EST
hi,
are you looking extension method kind of solution?
like string has inbuilt extension method called ToUpper(), ToLower() etc
Eric Brough3 reputationPosted Feb 13, 2014, 1:22 AM EST
I have a feeling that a Collection may be involved here - something I've not tackled before.
VulpesRank #55 · 29.2K reputationPosted Feb 13, 2014, 1:57 AM EST
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.