> public class ChildTest : ParentTest
> {
[quoted text clipped - 7 lines]
>
> }
just buble the return:
return this.TestMethod(TestEnum.A, s);
you can't cast from an interface to a type
regards
rjgeorge@zoominternet.net - 24 Aug 2007 19:01 GMT
On Aug 24, 1:52 pm, Horacio Nu?ez Hern?ndez <hnh12...@gmail.com>
wrote:
> > public class ChildTest : ParentTest
> > {
[quoted text clipped - 15 lines]
>
> regards
When I change to "return this.TestMethod(TestEnum.A, s);" as you
suggest, I get the following compile time error:
Error 1 Cannot implicitly convert type 'TestNamespace.ITest' to
'TestNamespace.ChildTest'. An explicit conversion exists (are you
missing a cast?)
Any suggestions?
Peter Duniho - 24 Aug 2007 19:07 GMT
> just buble the return:
>
> return this.TestMethod(TestEnum.A, s);
>
> you can't cast from an interface to a type
If that were true, your suggestion wouldn't work either, since the
return value is ChildTest, but TestMethod returns an ITest.
Fortunately, it's not true you can't cast from an interface to a type.
The problem here is that the instance isn't a ChildTest instance. It's
a ParentTest instance, and it's illegal to perform that cast.
Perhaps the OP meant to implement TestMethod in the ChildTest class as
well, and return a ChildTest instance from that method. The fact that
TestMethod is virtual suggests that might be the case. But without that
implemented override, the ParentTest.TestMethod is being called, and
that's returning a ParentTest instance, which of course cannot be cast
to a ChildTest instance.
Pete
Jon Skeet [C# MVP] - 24 Aug 2007 19:11 GMT
> > public class ChildTest : ParentTest
> > {
[quoted text clipped - 11 lines]
>
> return this.TestMethod(TestEnum.A, s);
That won't work while the GetData method is declared to return
ChildTest.
> you can't cast from an interface to a type
Yes you can, but only if the runtime object is of the appropriate type.
The interface is a red herring here - the problem is that TestMethod is
returning an instance of ParentTest, not an instance of ChildTest.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Horacio Nuñez Hernández - 27 Aug 2007 13:25 GMT
> Horacio Nu?ez Hern?ndez <hnh12...@gmail.com> wrote:
>
[quoted text clipped - 26 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
sorry about that
<snip>
> Below is a simple example of what I am trying to accomplish. Can
> someone tell me if I am way off base with this approach? If so, how
> would I set this up correctly?
The problem is that TestMethod is returning an instance of ParentTest,
not ChildTest - that's why the cast is failing. It's not got anything
to do with the interface, really.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too