0

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

    }
}
DrOstler
  • 21
  • 3
  • 3
    Side note: `newDeck` does not need to be `ref`, because you are not assigning to it. I suspect that a better signature would be `static List ShuffleNewDeck()`. – Blorgbeard May 09 '14 at 03:52

4 Answers4

6

Add public to your Card constructor.

Joel Allison
  • 1,951
  • 1
  • 10
  • 9
4

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.

Adriaan Stander
  • 150,765
  • 27
  • 267
  • 275
2

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

Community
  • 1
  • 1
Saghir A. Khatri
  • 3,341
  • 6
  • 40
  • 73
0

Your constructor isn't public.

public Card(int _value, string _suit)
Jane S
  • 1,387
  • 14
  • 21