Reputation: 610
For a poker parser (my first project for learning c#), I have a parent class clCardsHeld
and two child classes clHiHand
and clLoHand
. clCardsHeld
also contains properties and private fields, of each of the two child types. And so what I end up with, with this inheritance, is, eg:
public class clCardsHeld
{public clHiHand HiHand { Get {} Set {} }
}
public class clHiHand : clCardsHeld{}
clCardsHeld Hand3 = new clCardsHeld();
clHiHand hi = new clHiHand();
hi = Hand3.HiHand.HiHand.HiHand;
hi = Hand3.HiHand.LoHand.HiHand;
(as prompted by intellisense). (HiHand
shouldn't have another HiHand
as a property of itself, nor a low hand. But CardsHeld
has a HiHand
and LoHand
as properties in, eg, games where you get 7 cards and you can use 5 to make to best possible hand and 5 for the worst possible, both played at the same time, for half the pot each.)
I made the base class fields private, instead of protected, so the inherited property remains null
, if I've discerned that correctly. But really I'd like the property not to be inherited. Trying to research this, I see that a property can only be sealed when it overwrites a base property. And "hiding" the method generally refers to having different code in the child... .
One other thing, while I'm throwing out my novice questions:
public class clCardsHeld { private List<clCards> _liCards; }
public class clHiHand : clCardsHeld
I can find clCardsHeld._liCards
in the locals window, but I can't find clCardsHeld.clHiCards._liCards
in the locals window (but I can get at it by assigning it to a variable.
Thanks for any help.
Upvotes: 0
Views: 473
Reputation: 4019
There are a couple of misconceptions in your post..
I think you should split what is common to all the objects into a separate base class:
class HandBase { /* common to both HiHand an LoHand */}
class LoHand : HandBase {}
class HiHand : HandBase {}
From what I can tell from your explanation, cCardsHeld does not have to have the same base type:
class CardsHeld
{
public LoHand LoHand { get; private set;}
public HiHand HiHand { get; private set;}
}
But if it does, then you could do this:
class CardsHeld : HandBase
{
public LoHand LoHand { get; private set;}
public HiHand HiHand { get; private set;}
}
If there is a property that you don't want subclasses to have, then don't put it in the base class.
Upvotes: 1
Reputation: 2611
As mentioned in the link referenced by Adeel, the inheritance in this situation is unnecessary. Your high hand and low hands should just be new instances of clCardsHeld:
public class clCardsHeld
{
private List<clCards> _liCards;
public clCardsHeld GetLoHand()
{
//Logic here for returning the low hand, using _liCards
}
public clCardsHeld GetHiHand()
{
//Logic here for returning the high hand, using _liCards
}
}
The GetLoHand() and GetHiHand() functions will simply return a different set of held cards.
Upvotes: 1