Thread: two newbie questions concerning classes and things

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    two newbie questions concerning classes and things

    Hi. I recently started learning programming (again) and in my practice game that I've been concepting I ran into some problems.

    First, I would like to list all existing instances of a certain class. The class is a country in this game, and it's instances are of course Germany, France etc. What I'd want to achieve is that when the player is given a choice, I'd have some handy way of listing his targets, which would be the countries. Doing this manually for every choice and country would be an immense work and produce a lot of hard coding. Is it even possible to do this?

    Second, the country class has methods of course, and I thought that I had partially solved the problem of choosing targets with this trick

    Code:
    Console.WriteLine("Choose target country");
    string plrChoice = Console.ReadLine();
    (plrChoice).sampleAction();
    I thought that that would work but visual studio complains that it's a build error since string doesn't contain blah blah. So is it even possible to have the user fill name for class in code like that?

    Sorry if I'm confusing, just starting out this thing.

    ps. the "game" is just a simple console application

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Hmm actually if I want the game to list pre-set countries, I can do that with a string array and foreach loop. Tried doing it with a class array but didn't manage. My second question stands though, can I have the player input the name of the instance of country-class that'll have a method called from?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    So just to be sure

    You have some class like

    Code:
    class Test
    {
       void sample();
    }
    And you have a string variable

    Code:
    string className = "Test";
    And you want using variable className to call a method from the class Test?

    This is what you want to do?

    Is method static?

    Because otherwise you need an instance of the class first...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    I do have the instances properly spawned in this fashion

    Code:
    Country germany = new Country();
    Country being my class' name. I thought that would actually "name" the instances, and that I could then with some class array and foreach loop make the game display those names as a list. But from what I understand, that isn't possible?

    Normally I can call a method like this

    Code:
    germany.sampleMethod();
    but what I wanted to do was make the user able to choose from which instance a certain method is called. The methods change the class' variables so essentially player is supposed to choose what country their choice affects.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would create a dictionary of countries and use name as a key. Then I can use this dictionary to access country by name
    Code:
      countryList["Germany"].sampleMethod();
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why not include Name as a property of Country?

    Code:
    class Country
    {
        public string Name { get; private set; }
    
        public Country(string name)
        {
            Name = name;
        }
    
        public override string ToString()
        {
            return Name;
        }
    }
    And then you instantiate it like this:

    Code:
    Country germany = new Country("Germany");
    Now your players can each have their own list of countries:

    Code:
    class Player
    {
        private List<Country> _countries = new List<Country>();
        public List<Country> Countries { get { return _countries; } }
    }
    Now you're looking at some pretty usable classes:

    Code:
    Player player1 = new Player();
    Country germany = new Country("Germany");
    
    player1.Countries.Add(germany);
    
    foreach(Country country in player1.Countries)
        Console.WriteLine(country);
    If you understand what you're doing, you're not learning anything.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    To make it clear, in istme86 suggestion above, overriding the ToString() method is what will greatly facilitate the usage of this class, as is demonstrated in lines 6 and 7 of the last code snippet.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions on Classes
    By nosfearatu in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2010, 03:52 AM
  2. c++ classes newbie
    By johnnyboi in forum C++ Programming
    Replies: 2
    Last Post: 08-08-2007, 10:17 PM
  3. A few questions regarding classes
    By Xzyx987X in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2005, 03:42 PM
  4. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 06:49 AM
  5. some questions on classes
    By confuted in forum C++ Programming
    Replies: 15
    Last Post: 09-06-2002, 07:31 PM