public abstract class Sensor : Node
{
private PropertyClass _Properties;
public new class PropertyClass : Node.PropertyClass
{
public int Value;
public int UpdateInterval;
public SensorType Type;
public SensorModel Model;
}
public new PropertyClass Properties
{
get { return _Properties; }
set { _Properties = value; }
}
public enum SensorType
{
IR,
Sonar,
Touch,
Light,
PIR
}
public enum SensorModel
{
Sharp_GP2D12,
Ping_Sonar
}
public Sensor(string Name) : base(Name)
{
_Properties = new PropertyClass();
}
}
Now a subclass for an IR Sensor:
public class IR : Sensor
{
private PropertyClass _Properties;
public new class PropertyClass : Sensor.PropertyClass
{
public int MinRange;
public int MaxRange;
}
public new PropertyClass Properties
{
get { return _Properties; }
set { _Properties = value; }
}
public IR(string Name)
: base(Name)
{
_Properties = new PropertyClass();
this.Properties.Type = Sensor.SensorType.IR;
}
If I define a list of sensors like this:
List
And then I add an IR sensor:
mySensors.Add(new IR("My IR Sensor"));
Then I cannot get access to the MinRange and MaxRange properties of the sensor. For example, mySensors[0].MinRange is not defined because the compiler thinks mySensors[0] is just a generic Sensor type and doesn't know that it is the IR sub-type.
Is there a way I can make this work?
Thanks!
Jaish MathewsPosted May 11, 2010, 11:18 AM
Pi RobotPosted May 11, 2010, 10:43 AM
this.Properties.MySensors.Add(new Sharp_GP2D12("My Sharp IR Sensor");
foreach (RangeSensor sensor in this.Properties.MySensors)
{
Console.WriteLine(sensor.Properties.MaxRange.ToString());
}
And I get the correct value without having to specify that it is a Sharp_GP2D12 sensor in the foreach loop.
So the final RangeSensor, IR and Sharp_GP2D12 classes look like this:
public abstract class RangeSensor : Sensor
{
private PropertyClass _Properties;
public new class PropertyClass : Sensor.PropertyClass
{
public int MinRange;
public int MaxRange;
public int FieldOfView;
}
public new PropertyClass Properties
{
get { return _Properties; }
set { _Properties = value; }
}
public RangeSensor(string Name)
: base(Name)
{
_Properties = new PropertyClass();
}
public abstract class IR : RangeSensor
{
public new class PropertyClass : RangeSensor.PropertyClass
{
}
public IR(string Name) : base(Name)
{
this.Properties.Type = Sensor.SensorType.IR;
}
}
public class Sharp_GP2D12 : IR
{
public new class PropertyClass : IR.PropertyClass
{
}
public Sharp_GP2D12(string Name) : base(Name)
{
this.Properties.Model = SensorModel.Sharp_GP2D12;
this.Properties.MinRange = 4;
this.Properties.MaxRange = 31;
this.Properties.FieldOfView = 2;
}
}
Jaish MathewsPosted May 11, 2010, 2:54 AM
ONly need to adjust the foreach like below. No casting needed.
foreach (Sharp_GP2D12 sensor in MySensors)
{
Console.WriteLine(sensor.Properties.MaxRange.ToString());
}
NB:Look at the bolded red part. Based on storing type in side List
Pi RobotPosted May 10, 2010, 1:36 PM
Node->Sensor->RangeSensor->IR->Sharp_GP2D12
I'll list the Node and Sensor classes at the end of this message, but the new RangeSensor class looks like this:
public abstract class RangeSensor : Sensor
{
private PropertyClass _Properties;
public new class PropertyClass : Sensor.PropertyClass
{
public int MinRange;
public int MaxRange;
public int FieldOfView;
}
public new PropertyClass Properties
{
get { return _Properties; }
set { _Properties = value; }
}
public RangeSensor(string Name)
: base(Name)
{
_Properties = new PropertyClass();
}
And so my IR sensor class now looks like this:
public abstract class IR : RangeSensor
{
private PropertyClass _Properties;
public new class PropertyClass : RangeSensor.PropertyClass
{
}
public new PropertyClass Properties
{
get { return _Properties; }
set { _Properties = value; }
}
public IR(string Name) : base(Name)
{
_Properties = new PropertyClass();
Properties.Type = Sensor.SensorType.IR;
}
And my class for the Sharp IR sensor is:
public class Sharp_GP2D12 : IR
{
private PropertyClass _Properties;
public new class PropertyClass : IR.PropertyClass
{
}
public new PropertyClass Properties
{
get { return _Properties; }
set { _Properties = value; }
}
public Sharp_GP2D12(string Name) : base(Name)
{
_Properties = new PropertyClass();
this.Properties.Model = SensorModel.Sharp_GP2D12;
this.Properties.MinRange = 4;
this.Properties.MaxRange = 31;
this.Properties.FieldOfView = 2;
}
So now, using your idea, I want to be able to do something like this:
List
MySensors.Add(new Sharp_GP2D12("My Sharp IR Sensor");
foreach (Sensor sensor in this.Properties.MySensors)
{
if (sensor is RangeSensor)
{
Console.WriteLine(((RangeSensor)sensor).Properties.MaxRange.ToString());
}
}
However, when I do this, I get back 0 instead of 31. It seems that the value is coming from the default value of the RangeSensor class rather than being "propagated down" to the Sharp_GP2D12 class. Is there a way to make this happen without having to specifically down-cast to the Sharp_GP2D12 class?
Here now are the Node and Sensor classes:
public abstract class Node
{
private string _name;
private PropertyClass _Properties;
public Node(string Name)
{
_name = Name;
}
public Node()
{
}
public class PropertyClass
{
public Message.Status Status;
}
public PropertyClass Properties
{
get { return _Properties; }
set { _Properties = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public abstract class Sensor : Node
{
private PropertyClass _Properties;
public new class PropertyClass : Node.PropertyClass
{
public int Value;
public int UpdateInterval;
public bool Active;
public SensorType Type;
public SensorModel Model;
}
public new PropertyClass Properties
{
get { return _Properties; }
set { _Properties = value; }
}
public enum SensorType
{
IR,
Sonar,
Laser,
Touch,
Light,
PIR,
Temperature
}
public enum SensorModel
{
Sharp_GP2D12,
Rovio
}
public Sensor(string Name) : base(Name)
{
_Properties = new PropertyClass();
}
}
Jaish MathewsPosted May 10, 2010, 11:10 AM
I got access to you properties using below foreach
foreach (IR irObj in mySensors)
{
Console.WriteLine( irObj.Properties.MaxRange.ToString());
}
NB:But You never given your "Node" class definition. So doubting about the same solution in your code.
Pi RobotPosted May 10, 2010, 10:08 AM
(Sensors.IR)this.Properties.MySensors[0]).Properties.MinRange
However, this requires that I have the child's class type hard coded on the line above which I'd rather not do.
Amit ChoudharyPosted May 10, 2010, 1:42 AM
make your MinRange and MaxRange property of sensor class protected or public..
and Implement the indexer inside your Sensor class to access the properties and method using Collection index instance e.g., MySeson[0].MinRange
Please mark as "Do you like this post" if it helps you.