Re: Inheritance Problem
- From: "Gaz" <a@xxxxx>
- Date: Mon, 30 Apr 2007 08:22:11 +0100
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.209f9ef9718717b12c@xxxxxxxxxxxxxxxxxxxxxxx
Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote:
What I want is for player[i] to 'be' a PokerPlayer within the
PokerTable
class without having to cast it every time. I wouldn't mind so much if
CardPlayer[] player could be cast to PokerPlayer[] but I can't get that
to work.
Any ideas how to implement this idea?
The simplest method I see based on your current design would be to add a
new property that you use to alias the original array:
<snip>
Alternatively, you could provide a method that retrieves a specific index
and does the casting for you:
<snip>
If you're using .NET 2.0, you could make CardTable generic:
public class CardTable<T> where T : CardPlayer
then use "T" everywhere in CardTable, and declare PokerTable like this:
public class PokerTable : CardTable<PokerPlayer>
Thanks guys, I like the generic idea I will try that and see how far I get
with it.
Btw using a property to alias the original array like this:
public PokerPlayer[] pokerplayers
{
get { return (PokerPlayer[])player; }
}
doesn't work, I already tried it a few times in different forms. It
compiles ok but I get an InvalidCastException at runtime, 'Unable to cast
object of type 'CardPlayer[]' to type 'PokerPlayer[]'.'
.
- Follow-Ups:
- Re: Inheritance Problem
- From: Peter Duniho
- Re: Inheritance Problem
- References:
- Inheritance Problem
- From: Gaz
- Re: Inheritance Problem
- From: Peter Duniho
- Re: Inheritance Problem
- From: Jon Skeet [C# MVP]
- Inheritance Problem
- Prev by Date: Re: [OT] - Svn Multiple Repositories on Windows
- Next by Date: Handling Cancel on Form Close
- Previous by thread: Re: Inheritance Problem
- Next by thread: Re: Inheritance Problem
- Index(es):
Relevant Pages
|