Jump to content
  • Advertisement
Sign in to follow this  
dechorus

c# good practice code

This topic is 2998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everybody.

I've tried searching, and have not found an answer to my question. So I thought of posting here.

Anyway, here is my situation.

Suppose I have a Country class and a City class. A country class has a name (string) and a list of cities (List<City>).

Now is there a way, through inheritance or any other way, for me to get the Country from a City class? (i.e. the country it belongs to) Or am I supposed to have and manually create a Country variable for each City that I create? What's the best OO method for getting this?

Thanks to whoever can help :)

[Edited by - dechorus on July 4, 2010 8:27:46 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hm I'm not sure if I was clear enough, so I'll give another example.


List<City> cities = new List<City>();

cities.Add(new City("Cairo"));
cities.Add(new City("Alexander"));

Country Egypt = new Country("Egypt", cities);


Suppose it's a game and I can travel from city to city. Knowing the current city that I'm in, is there a way to, from that information, get the country I'm in?

As I've mentioned before, I thought of having a Country variable for each City. But that confuses me, since the declaring of a Country requires a List of Cities.

And that's where my question on what the best method would be in this case, through inheritance or otherwise.

Again, thanks :)

Share this post


Link to post
Share on other sites
Quote:
Original post by dechorus
Hm I'm not sure if I was clear enough, so I'll give another example.


List<City> cities = new List<City>();

cities.Add(new City("Cairo"));
cities.Add(new City("Alexander"));

Country Egypt = new Country("Egypt", cities);


Suppose it's a game and I can travel from city to city. Knowing the current city that I'm in, is there a way to, from that information, get the country I'm in?

As I've mentioned before, I thought of having a Country variable for each City. But that confuses me, since the declaring of a Country requires a List of Cities.

And that's where my question on what the best method would be in this case, through inheritance or otherwise.

Again, thanks :)


I'm not sure that your 'city' class really needs to inherit from 'country' if that's what you're doing. Is there some specific reason you're doing it that way? If there is no function that the city class needs from the country class I would say there's no reason for you to inherit it.

It seems like you would want to also have a list of countries, therein each country contains a list of cities. You could have a function called GetCountry or something in the country class that returns a string or just a console.writeline(this.country). If you need to access cities you simply access the list inside a specific country object: I.E. Egypt.cityList.function();

Pay specific attention to the public, protected, and private modifiers also.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalnos
I'm not sure that your 'city' class really needs to inherit from 'country' if that's what you're doing. Is there some specific reason you're doing it that way? If there is no function that the city class needs from the country class I would say there's no reason for you to inherit it.

It seems like you would want to also have a list of countries, therein each country contains a list of cities. You could have a function called GetCountry or something in the country class that returns a string or just a console.writeline(this.country). If you need to access cities you simply access the list inside a specific country object: I.E. Egypt.cityList.function();

Pay specific attention to the public, protected, and private modifiers also.


Thanks for that. I was actually using a List, and I've sort of found a solution.
Yes, there is not a need for inheritance I realised, as none of the functionality of the child is being used by the parent. I had thought I could access the Name of the Country object from the City object - that's the line of thinking I was going through.

I suppose this is more a question on good practice code, rather than inheritance. I've changed the name of the topic.

This is my current solution. Any comments are welcome.

// X value is the current city the player is in. Y is the country.
Point playerPos = startingPos;
// contains a name and a list of Cities.
List<Country> Countries = new List<Country>();
// this is a method that loads in countries and cities from a text file.
LoadCountries();

// Then, if I want to get the country I'm in, I simply write

Countries[playerPos.Y].Name;

// For City, I write this

Countries[playerPos.Y].Cities[playerPos.X].Name;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!