Reputation: 21
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
}
}
Upvotes: 0
Views: 1029
Reputation: 3128
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
Upvotes: 1
Reputation: 1487
Your constructor isn't public.
public Card(int _value, string _suit)
Upvotes: 0
Reputation: 166396
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.
Upvotes: 4