tags:

views:

65

answers:

3

I'm a PHP programmer by default, and I need a little help with a C# .NET (Micro Frameworks, but that doesn't matter here) project.

Basically, I have a class that allows for control of an RC car or airplane speed controller. The class is already written, but I need a way to import settings into the class, since diffident electric speed controllers require different settings.

My ideal set up would be something like this:

SpeedController esc = new SpeedController(SpeedControllers.TRAXXAS_XL5);

I know how the constructor and all that work, but how do I configure the SpeedControllers.TRAXXAS_XL5 part? I would create a base class (Speedcontrollers) and then a class that extends it, overriding the default values, correct?

Can someone direct me to a tutorial on what I am talking about or a small code snippet of a child class overriding properties in a parent class?

+2  A: 

IMO, best way to do that would be something like this:

public struct SpeedControllers {
    int speed;
    int etc;

    public SpeedControllers(int s, int e) {
        speed = s;
        etc = e;
    }

    public const SpeedControllers TRAXXAS_XL5 = new SpeedControllers(123, 345);
    public const SpeedControllers WHATEVER = new SpeedControllers(456, 789);
}

And, have the SpeedController constructor read the values out of the struct. Obviously, add/customize the fields as necessary.

DISCLAIMER: I haven't run this through the compiler, only through my brain. Do not attempt to fly an actual airplane with this as is ;)

Mike Caron
lol for the disclaimer, although I have a feeling that SpeedControllers.TRAXXAS_XL5 refers to an internal class/struct, or perhaps an enum.
Robert Harvey
This is in no way meant a knock against the CLR. Windows, or Microsoft but please don't use the Micro Framework on any critical component of a "real" airplane... It's not a realtime OS. As for your code you should probably consider static readonly over a constant.
Matthew Whited
Don't worry, it's not for an airplane. If it was, it wouldn't be on NETMF
chris12892
@Robert: I get the impression that we're coming up with new code here. I.e., there is no existing TRAXXAS_XL5 struct.
Mike Caron
+1  A: 

If SpeedControllers.TRAXXAS_XL5 is nothing but configuration data (i.e., all numeric values) then I wouldn't consider this an optimal example of derived classes.

Yes, you can create a base class (SpeedController) which contains the properties and perhaps default values. Then you could create derived classes that override.

Instead, what Mike Caron has demonstrated is using the same class and simply using initialization of the data to create the various specialized instances of the controller class. I would recommend something like that (perhaps loading the values from an external file if you want to make it easy to configure).

Godeke
But what if there are three different ways to instantiate a SpeedController? I don't imagine this would work very well for makes and models of vehicles, but it might work if you want to differentiate a train `SpeedController` from a truck `SpeedController`, and set up some default object characteristics accordingly.
Robert Harvey
I can't load from file, it's embedded. I would generally agree, that yes, this would be something that should NOT be hard coded, but in this case, it is.
chris12892
@Godeke: Obligatory link: http://thedailywtf.com/Articles/The-cbitmap.aspx (Read the comments, and then roll your eyes)
Mike Caron
Jeez, it's like the Godwin's law of TDWTF
chris12892
+2  A: 

I'll just take an example using a hypothetical MaxSpeed the conroller might have.

abstract class SpeedControllers {
    public abstract int MaxSpeed { get; }

    public class TRAXXAS_XL5 : SpeedControllers{
        public override int MaxSpeed {
            get {
                return 30;
            }
        }
    }
}

Then in your SpeedController constructor

class SpeedController {
    readonly SpeedControllers properties;
    public SpeedController(SpeedControllers properties) {
        this.properties = properties;
    }
    public int MaxSpeed {
        get {
            return properties.MaxSpeed;
        }
    }
 }

Call it with new SpeedController(new SpeedControllers.TRAXXAS_XL5())

Mark H
Awesome. This is exactly what I needed. Thanks!
chris12892