How to cast class which implements an interface to a subclass

R

rjgeorge

I have a scenario where I have created a parent class, ParentTest,
which implements an interface, ITest. The ParentTest class has a
method which returns an instance of the interface type. Each child
class has a method which calls the parent's TestMethod method in order
to simplify method calls and make them more intuitive for each sub
type for developers.

In the ChildTest class, I want to return an instance of the ChildTest
in the GetData method. However, I receive the following error:

An unhandled exception of type 'System.InvalidCastException' occurred
in test1.exe

Additional information: Unable to cast object of type
'TestNamespace.ParentTest' to type 'TestNamespace.ChildTest'.

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?

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TestNamespace
{
public enum TestEnum { A, B, C, D }

public interface ITest
{
string Code {get;set;}
string Description {get; set;}

// Define simple method
ITest TestMethod(TestEnum e, string s);
}

public class ParentTest : ITest
{
string _code;
string _description;

public ParentTest() : base() { }
ParentTest(string e, string s)
: base()
{
_code = e;
_description = s;
}

public string Code {
get
{
return _code;
}
set
{
_code = value;
}
}

public string Description {
get
{
return _description;
}
set
{
_description = value;
}
}

public virtual ITest TestMethod(TestEnum e, string s)
{
// TODO: Create a method to do a look up by type and code
as passed by enum value and string
Console.WriteLine("\n\nIn TestMethod. e: " + e.ToString()
+ " s:" + s + "\n");
ParentTest pt = new ParentTest(e.ToString(),
s);
ITest i = pt as ITest;
return i;
}
}

public class ChildTest : ParentTest
{
public ChildTest() : base() { }

public ChildTest GetData(string s)
{
ITest i = this.TestMethod(TestEnum.A, s);
return (ChildTest)i;
}

}

static class Program
{
[STAThread]
static void Main()
{
ChildTest f = new ChildTest();
ChildTest c = f.GetData("TestLookupCode");
Console.WriteLine("\n\nCode: " + c.Code + " Description: "
+ c.Description + "\n\n\n");
}
}
}
 
?

=?iso-8859-1?q?Horacio_Nu=F1ez_Hern=E1ndez?=

public class ChildTest : ParentTest
{
public ChildTest() : base() { }

public ChildTest GetData(string s)
{
ITest i = this.TestMethod(TestEnum.A, s);
return (ChildTest)i;
}

}

just buble the return:

return this.TestMethod(TestEnum.A, s);

you can't cast from an interface to a type

regards
 
R

rjgeorge

just buble the return:

return this.TestMethod(TestEnum.A, s);

you can't cast from an interface to a type

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?
 
P

Peter Duniho

Horacio said:
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
 
J

Jon Skeet [C# MVP]

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.
 
J

Jon Skeet [C# MVP]

Horacio Nuñez Hernández said:
just buble the return:

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.
 
?

=?iso-8859-1?q?Horacio_Nu=F1ez_Hern=E1ndez?=

That won't work while the GetData method is declared to return
ChildTest.


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.

sorry about that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top