I have a list of base objects (RTUDevice) and want to iterate through and convert each to a derived object (actually a derived of a derived RTDSensor) , however the downcasting is throwing an error.
public RTUDevice(int id)
{
_id = id;
}
public class RTDDevice : RTUDevice
{
public RTDDevice(int id)
: base(id)
{
}
}
public class RTDSensor : RTDDevice
{
public RTDSensor(int id)
: base(id)
{
}
}
RTDSensor return = (RTDSensor)_devices.Find(d => d.Id == p.ReturnId);
Would it be better to pass the base object in a constructor to RTDSensor like
public RTDSensor(RTUDevice rtu) : base(rtu.Id)
{
}
or is my OOP design way off the mark.
The problem could be with the way you're adding the devices to the _devices collection.
For example:
RTDSensor sensor = new RTDSensor(1);
_devices.Add(sensor);
RTDSensor retrievedSensor = (RTDSensor)_devices.Find(d => d.Id == 1);
should work. However if you add the sensor as an RTUDevice object you can't cast it to RTDSensor afterwards because it doesn't have the additional information that RTDSensor carries.
Referring to Jon Skeet's example you have to use
object o = new FileStream(path, filemode);
FileStream fs = (FileStream)o;
If you later want to cast o to a FileStream object.