i just can't figure out why something im doing is not
working correctly....
public interface IInterface
{
int someProperty
{
get;
set;
}
}
public abstract class II_interface : IInterface
{
private int m_someProperty;
public int someProperty
{
get
{
return m_someProperty;
}
set
{
m_someProperty = value;
}
}
}
public class myDerivedClass : II_interface
{
private int m_someOtherProp erty;
public int SomeOtherProper ty
{
get
{
return m_someOtherProp erty;
}
set
{
m_someOtherProp erty = value;
}
}
public int SomeImplemented Property
{
get
{
return base.SomeProper ty*2;
}
}
}
public class myDerivedClass2 : II_interface
{
private int m_someOtherProp erty2;
public int SomeOtherProper ty2
{
get
{
return m_someOtherProp erty2;
}
set
{
m_someOtherProp erty2 = value;
}
}
}
public class Test
{
myDerivedClass c1 = new myDerivedClass( );
myDerivedClass2 c2 = (myDerivedClass 2)c1; //
EXCEPTION: Specified cast is not valid.
IInterface c3 = new myDerivedClass( );
myDerivedClass2 c4 = (myDerivedClass )c3;//
invalid cast
}
my question is why am i getting the invalid cast
exception. If this is not possible, how can i make it
possible.
What i'm trying to do, is
1) make one class with properties.
2) derive several other class from the first class, each
class having it's own set of properties (and since im
deriving, all the properties from the first class)
3) create an array of the first class
4) for each element in array, based on a unigue type or
enum, cast the element to the correct derived class.
This kinka like a factory pattern, but i didn't want to
implement the factory.. i just want to be able to cast,
so I can set up the first classes' properties, then use
the derived classes to access the already setup variables
in the first class. the first class will be the only
class with a public contructor. The rest of the derived
classes will be casted, to use the base classes already
created/filled properties.
Thanks
Kurt Lange
working correctly....
public interface IInterface
{
int someProperty
{
get;
set;
}
}
public abstract class II_interface : IInterface
{
private int m_someProperty;
public int someProperty
{
get
{
return m_someProperty;
}
set
{
m_someProperty = value;
}
}
}
public class myDerivedClass : II_interface
{
private int m_someOtherProp erty;
public int SomeOtherProper ty
{
get
{
return m_someOtherProp erty;
}
set
{
m_someOtherProp erty = value;
}
}
public int SomeImplemented Property
{
get
{
return base.SomeProper ty*2;
}
}
}
public class myDerivedClass2 : II_interface
{
private int m_someOtherProp erty2;
public int SomeOtherProper ty2
{
get
{
return m_someOtherProp erty2;
}
set
{
m_someOtherProp erty2 = value;
}
}
}
public class Test
{
myDerivedClass c1 = new myDerivedClass( );
myDerivedClass2 c2 = (myDerivedClass 2)c1; //
EXCEPTION: Specified cast is not valid.
IInterface c3 = new myDerivedClass( );
myDerivedClass2 c4 = (myDerivedClass )c3;//
invalid cast
}
my question is why am i getting the invalid cast
exception. If this is not possible, how can i make it
possible.
What i'm trying to do, is
1) make one class with properties.
2) derive several other class from the first class, each
class having it's own set of properties (and since im
deriving, all the properties from the first class)
3) create an array of the first class
4) for each element in array, based on a unigue type or
enum, cast the element to the correct derived class.
This kinka like a factory pattern, but i didn't want to
implement the factory.. i just want to be able to cast,
so I can set up the first classes' properties, then use
the derived classes to access the already setup variables
in the first class. the first class will be the only
class with a public contructor. The rest of the derived
classes will be casted, to use the base classes already
created/filled properties.
Thanks
Kurt Lange
Comment