0

I'm basically programming a card game. This one has class named c_Card, which has Card properties (such as name, cost etc).

I'm trying to create an instance of this class for each card I generate, like:

// Constructor:
public c_card(string name, int cost)
    {
        Name = name;
        Cost = cost;
    }
// Creating the object:
c_card c1 = new c_card("CardName", 2);

So I'm wondering if I've to create a new object for each card, and so, how to increment the object name, or is there a better option to manipulate and retrieve my cards?

Many thanks in advance.

Necstreeam
  • 23
  • 3
  • 1
    Create new object - yes. Create new variable - not necessarily, you'd want to use arrays or dictionaries or other containers (depending on how exactly you use the objects) – UnholySheep Jun 17 '22 at 10:32
  • Similar question (already closed) https://stackoverflow.com/questions/72657843/what-language-can-do-it-creating-variables-and-there-names-dynamically or its duplicate https://stackoverflow.com/questions/20857773/create-dynamic-variable-name – Hans Kesting Jun 17 '22 at 11:12
  • Hello, many thanks for your answers. However, it seems there is a confusion here. My program is linked to a MySQL Database and I'm able to retrieve the name of cards and put them in parameters. However, I would like to create an object of the class c_Card, for each card that I'll generate. It means that the name of the object might be different each time. So, is it possible to set the name of an object, as the content of my first dictionary column? This way, I could have a different name for each object. – Necstreeam Jun 17 '22 at 12:35
  • So you get a (unique?) name already from the database? Of course you can use that as property value and/or a dictionary key – Hans Kesting Jun 17 '22 at 15:27
  • Hello Hans, thanks for your answer. I'm able to, in fact, insert as parameter the name of cards, which are unique in my DB. I'm currently trying to generate a hand of cards (with let's say 7 cards). Is that a good thing to generate 7 distinct objects? In this case, I'll need to have 7 different object names. I've some troubles understanding how to manipulate my object (cards) in the future, if they don't have a unique name. – Necstreeam Jun 17 '22 at 16:26
  • Allright, I made some researches and indeed, dictionary seems to be the real deal here. I've a generation class that will permit me to get an incremented ID every-time I extract a card from the DB. So what I'll do, it's that I'll store them in a dictionary, with this ID as KEY, and others columns would be the parameters of the card (name, cost etc...) – Necstreeam Jun 17 '22 at 16:55

0 Answers0