R
Rein Petersen
Hi Folks,
I've accepted that one cannot downcast between types, however I can't find a
way to achieve
//...
class Employee
{
public string name;
}
class ContractEmployee : Employee
{
public string contractID;
}
class SomeOtherKindofEmployee : Employee
{
public string relevantAttribute;
}
class SampleProgram
{
public static void Main()
{
//we need to create the employee object and work with it
Employee e = new Employee();
e.name = "Fred";
//later, we need to specify the Employee as a ContractEmployee
// but the operation will fail because it is a down-level cast.
ContractEmployee c = (ContractEmployee) e;
c.contractID = "1234";
}
}
//...
How else would I create the 'ContractEmployee' object without having to
explicity copy the value's from all of the Employee object's properties to
the newly created ContractEmployee object's properties? I mean, it seems to
me that the 'ContractEmployee' object is just an extension of the 'Employee'
object so why cant I just add properties to an object by changing it's type
to a downlevel (child) type?
Rein
I've accepted that one cannot downcast between types, however I can't find a
way to achieve
//...
class Employee
{
public string name;
}
class ContractEmployee : Employee
{
public string contractID;
}
class SomeOtherKindofEmployee : Employee
{
public string relevantAttribute;
}
class SampleProgram
{
public static void Main()
{
//we need to create the employee object and work with it
Employee e = new Employee();
e.name = "Fred";
//later, we need to specify the Employee as a ContractEmployee
// but the operation will fail because it is a down-level cast.
ContractEmployee c = (ContractEmployee) e;
c.contractID = "1234";
}
}
//...
How else would I create the 'ContractEmployee' object without having to
explicity copy the value's from all of the Employee object's properties to
the newly created ContractEmployee object's properties? I mean, it seems to
me that the 'ContractEmployee' object is just an extension of the 'Employee'
object so why cant I just add properties to an object by changing it's type
to a downlevel (child) type?
Rein