Suppose I have an abstract base class Deck:
public abstract class Deck
{
public List<Card> cards;
public Deck(string[] values, string[] suits)
{...}
...
}
and a derived class EuchreDeck:
public class EuchreDeck : Deck
{
string[] values = new string[] { "9", "10", "J", "Q", "K", "A" };
string[] suits = new string[] { "clubs", "spades", "hearts", "diamonds" };
public EuchreDeck() : base(values, suits) // Error.
{}
...
}
I want the ability to instantiate EuchreDeck
and have the two string arrays passed to the base class, i.e. var gameDeck = new EuchreDeck();
.
Currently I'm getting the error: "An object reference is required for the non-static field, method, or property EuchreDeck.values
."
Is this possible, or will calling the derived default constructor always call the base default constructor?
I think the problem is that the values and suits should be declared static.
That way, they will be available at when instantiating the new class.
Yes, you can do this if you make the arrays
static
:The reason why you can't use it as you had with instance-level members is because it's not legal to do so. This comes from the C# specification 10.10.1 Constructor Initializers where it states:
By switching the arrays to be
static
, they are no longer accessed via the instance but rather by theEuchreDeck
type.That said, I might suggest you take a slight tweak on the design. Maybe use a factory to create these specialized decks for you rather than their constructors.
As an example, maybe refactor something like this:
Change your base
Deck
to just take the set of cards:Then have the factory setup like this:
Instantiation/usage as:
You could play around with the factory usage. I just nested it in the class so you couldn't create a
EuchreDeck
with an invalid set of cards. YourDeckFactory
base would have your conversion method (which looks like you currently have in yourDeck
constructor)Beyond that, I'm not sure if you have a specific need for a
EuchreDeck
; I'm assuming you have other methods associated with it? If not, you could probably ditch the class altogether and just let the factory create aDeck
with the needed cards.You have to have somewhere that variable decalred :
where are
values
andsuits
declared?You have to define them
static
, like:The thing is that
base(..)
can only access values visible in ctor scope. So you can not pass instance variables, as instance is not accessible on that level, but you can pass ctor parameters and static members of calling class.