Reputation: 3893
I'm trying to think through a new class I'm trying to implement, and have an idea that I'm not sure is good or bad. I want to create a class that holds device settings (ex. inch vs. metric) as well as codes that correspond to the settings. I think it would be nice to have code that looks like this:
Device myDevice = new Device();
myDevice.units = Device.Inches;
myDevice.MoveTo(1,2,3, Device.Rapid);
and the Device class file would be:
class Device
{
public static DeviceUnit Inches = DeviceUnit("G21");
public static DeviceUnit Metric = DeviceUnit("G20");
public static DeviceMovement Rapid = DeviceMovement("G00");
public static DeviceMovement Feed = DeviceMovement("G01");
public DeviceUnit units;
public Device()
{
// Default to metric system
units = Device.Metric;
}
public Device(DeviceUnit customUnit)
{
units = customUnit;
}
public MoveTo(float x, float y, float z, DeviceMovement movement)
{
string command = string.Format($"{units.gcode} {movement.gcode} ");
command += string.Format($"X{x} Y{y} Z{z}\r\n");
Debug.Write(command);
}
}
Device Unit struct:
public struct DeviceUnit
{
public string gcode;
public DeviceUnit(string code)
{
gcode = code;
}
}
DeviceMovement struct:
public struct DeviceMovement
{
public string gcode;
public DeviceUnit(string code)
{
gcode = code;
}
}
My worry is I might end up being 'overkill' on the amount of structs I use. Already I'm thinking I should make another to store Incremental (G90) vs Absolute (G91) positioning. I'd like to make this flexible so that in the future I can load the gcode
strings from an XML configuration file so that I can quickly create new XML files for new machine configurations.
Is using multiple structs too overkill for this task?
Should I combine the structs together somehow?
Upvotes: 0
Views: 479
Reputation: 11032
The struct have a meaning if it has multi properties that represent complex object.
I find that your struct DeviceUnit, DeviceMovement are only one property of type string, so why struct ?
let DeviceUnit, DeviceMovement string property. but wait :)
Q: Is using multiple structs too overkill for this task?
A: No, Struct is not overkill if it is used to describe an object (which may be complex device property) with many properties.
example:
public struct Dimension
{
//avoid using constructor. You can initialize with object initializer
public int x;
public int y;
public int z;
}
for example: All devices of windows are stored in WMI classes like The Win32_Printer WMI which has more than 40 property, and most of properties are a complex object.
q: Should I combine the structs together somehow?
A: simply you define a class named Device which have properties and method. If one of the properties is a complex object, it should be of type struct or class. You build Object model for the device , so select the type of the properties carefully. but in your code , really your are not in need to the struct at all , use simple properties like:
public static string Inches {get;set;} = "G21"; // in c#6 you can initialize properties
My Question: Why Static properties?
My Question: Why you initialize properties with default values.
A: You can create xml file for every device and load it during object instantiation, and this give you more capability:
Use one class (or more specialized classes) to represent your device You can add the following method to your device class:
public LoadDevice(string xmlFilename)
{
// read xml file , e.g Linq to xml
// set properties
}
Here your ceiling is the sky :)
BTW, you should use new keyword if the struct has constructor. so it should:
public static DeviceUnit Inches = new DeviceUnit("G21"); //:)
Upvotes: 1