Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Can I automatically create infinite instances of a class?

Lodeon003Lodeon003 Member
edited September 2020 in Programming

Basically I want to make a class that rappresents some cards for a card game.

Since I would have to create every card manually changing the name of the card with this code: [Card 01 = new Card] is there a way to change the name automatically (01)?

I just started learning programming so it might be a stupid question

Best Answer

  • Sl0thSl0th Member
    Accepted Answer

    Hmm, I don't quite understand why you'd have to do it manually with an array. Do you know about for loops?

    int numOfCards = 52;

    Card[] cards = new Card[numOfCards];

    for (int i = 0; i < numOfCards; i++) // i starts at 0 and goes up by one until i reaches numOfCards. Each time it goes up, this loop is run.

    {

    // In here you can do whatever you want with i.

    cards[i] = new Card();

    cards[i].value = i;

    }

    I'm sorry if I'm getting the problem wrong.

Answers

  • Not that I am aware of, but why not make an array of them?

  • O could, but I would have to store the color of every card in a variable or another array..

  • Why not have it be part of the Card class? And how could you avoid that with your original suggestion?

  • Because by using the card class and putting the instances of the class (cards) in an array, I would have to add every card manually, so I was searching for a method to create cards automatically...

    ==========================

    while(stop != true) {

    Card [variable to change the name] = new Card;

    [Still that variable for the name].value = Card.lastValue;

    Card.lastValue++;

    }

  • I didn't have space to write... So it puts the last value of the card, and then adds 1 so every card would have an increased number of one.. I just don't know how to write the Card [name] = new Card command to create more cards but suing the command once

  • Oh so the command to create an instance is name = new card()?

    If to crate a card I need that command the problem is solved... I messed up and confused the commands then...

    Thanks for the help

  • Alright, no worries.

  • Ok I wrote that program and it should be right, basically I created a card class, an array and I was putting in the array the cards like you said, with

    array [nextFreeSlotVariable] = new Card();

    But the problem is that the console says "can't convert <file name>.Card into int"

    I tried changing the type of the array but it doesn't work... For now I have put 2 variables in the card class, the value and color.... Should I put them in separated arrays?

    Maybe I can't put 2 values in the array at same time

  • Nevermind this was a stupid question... To store in arrays the value and the color I just needed to make 2 different arrays and move them same way to move the cards...

Sign In or Register to comment.