0

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?

Xander Luciano
  • 3,753
  • 7
  • 32
  • 53
  • Aside from anything else, it's not at all clear what `gcode` is in either case. For units, you probably want an enum instead. In all cases, I'd suggest using private fields - and in almost all cases structs should be immutable, too. – Jon Skeet Aug 04 '16 at 21:45
  • @JonSkeet gcode is machine language (for CNC's, 3D printers, etc). But enum is what I was looking for! I kept finding information on dictionary, but I prefer the `Device.Inches` approach over `Device.units.Value["inch"]` syntax. Lastly I was reading up on structs and it seems that the `readonly` keyword doesn't actually work on structs? I just skim read this question: http://stackoverflow.com/questions/6063212/does-using-public-readonly-fields-for-immutable-structs-work – Xander Luciano Aug 04 '16 at 21:50
  • 1
    Well `readonly` "works" in terms of what the language permits, but there are ways of modifying things behind the scenes. I'd still suggest making fields in structs readonly so that you don't change them within the struct, even if they could be changed externally. – Jon Skeet Aug 04 '16 at 21:52
  • Gotcha, if I make it readonly, is it proper to use the `readonly` keyword or is the preferred method `private string _gcode` and `public string gcode { get { return _gcode; } }`? and just set the value in the initialization? – Xander Luciano Aug 04 '16 at 21:56
  • I'd use both - if you need to expose it at all, use a property. (C# 6 makes that easier, mind you.) You should read up on .NET naming conventions though. – Jon Skeet Aug 04 '16 at 21:59
  • Thanks! If you wanna type up a simple answer with a short example or so I'll go ahead and accept it. – Xander Luciano Aug 04 '16 at 22:01

1 Answers1

1

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");  //:)
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • Shortly after I started implementing XML based config and created a `DefaultSettings.xml` and made one single struct with 8 different strings for all the different codes :) And good catch on the new keyword! Exactly what I was looking for though. Thank you. – Xander Luciano Aug 05 '16 at 20:01
  • Welcome. Glad for your success in solving the problem. – M.Hassan Aug 05 '16 at 20:57