1

I'm not sure if the title is clear so, I'm declaring an array in class, but the problem is that when I want to create a class the size of array might be different and by that I want to tell the size of array to the class by the constructor. Example.

    const short DeckSize;
    private char[,] Deck = new char[DeckSize, 2];
    public HandDeck(short Size)
    {
        DeckSize = Size;
    }

So the problem here is that array has to be a constant or static as my compiler says.

I found something about readonly attribute and tried to change code to this

readonly short DeckSize;
private char[,] Deck = new char[DeckSize, 2];

public HandDeck(short Size)
{
     DeckSize = Size;
}

And it works (probably), but now the declaration doesn't meet the requirements. And at this point I am out of ideas. Any great suggestions here?

1 Answer 1

3
    private readonly short DeckSize;
    private readonly char[,] Deck;
    public HandDeck(short Size)
    {
        DeckSize = Size;
        Deck  = new char[DeckSize, 2];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Oh great, that was quite easy. Thanks for fast reply.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.