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");
}
}
}
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");
}
}
}