Question

Im trying to add cards to a list. The cards are structs. In my struct I have a constructor that takes a value and a suit. When I try to create a new card, I get the error "'Blackjack.Program.Card' does not contain a constructor that takes 2 arguments" As you can see though, it does. This is my code.

public struct Card
{
    int value;
    string suit;

    Card(int _value, string _suit)
    {
        value = _value;
        suit = _suit;
    }
};

public List<Card> deck = new List<Card>();


static void shuffleNewDeck(ref List<Card> newDeck)
{
    newDeck.Clear();

    for (int i = 0; i < 13; i++) // add spades
    {
        newDeck.Add(new Card(i, "Spade")); // error here

    }
}
Was it helpful?

Solution

Add public to your Card constructor.

OTHER TIPS

Change the constructor accessor to public

something like

public struct Card
{
    int value;
    string suit;

    public Card(int _value, string _suit)
    {
        value = _value;
        suit = _suit;
    }
}

From Private Constructors (C# Programming Guide)

Note that if you do not use an access modifier with the constructor it will still be private by default.

Structs and classes in c# are by default are private. you have to modify your code Like:

public struct Card
{
    int value;
    string suit;

    public Card(int _value, string _suit)
    {
        value = _value;
        suit = _suit;
    }
};

EDIT

I have changed my answer with reference to this post

Your constructor isn't public.

public Card(int _value, string _suit)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top