0

I'm venturing into new territory as far as my normal coding habits; I'm creating structs to handle some of my applications basic data types.

I have a struct for JoyAxis which contains info for a single axis, then a struct for XYJoy which contains info for several JoyAxis(s), I want to be able to define values for JoyAxis inside XYJoy without having to reconstruct the entire axis.

For example if JoyAxis has values for current, min, and max; assuming X is JoyAxis inside MyJoy which is XYJoy, I want to be able to set the (current) value by MyJoy.X = 23 but still maintain MyJoy.X.min and other struct info.

It occurs to me I could override get /set of a field that points to it (again new territory for me so i'm not sure that terminology is correct) but being so new to this I wanted to know if there is a more common way to do this.

    public struct JoyAxis
    {
        int Value { get; set; }
        int max_Value { get; set; }
        int min_Value { get; set; }
        int center_Value
        {
            get
            {
                return (int)Math.Ceiling((double)(this.max_Value - this.min_Value) / 2d);
            }
        }
        public JoyAxis(int value)
        {
            this.Value = value;
        }
        public JoyAxis(int value, int min_value, int max_value)
        {
            this.Value = value;
            this.min_Value = min_value;
            this.max_Value = max_value;
        }

        static public implicit operator JoyAxis(int value)
        {
            return new JoyAxis(value);
        }
    }
    public struct XYJoy
    {
        JoyAxis X { get; set; }
        JoyAxis Y { get; set; }
    }
Wobbles
  • 3,033
  • 1
  • 25
  • 51
  • 'new territory' as instead of using classes or is this your first step into structs/classes at all? Because I would strongly advice `class` here. – H H Feb 26 '15 at 11:51
  • 1
    Mutable structs are generally a bad idea, see for example http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil – Rik Feb 26 '15 at 11:51
  • 1.First problem Mutable structs, 2. Why do you even need implicit conversion from int? Why can't you generate a new struct instance and set it? – Sriram Sakthivel Feb 26 '15 at 11:52
  • I have used classes in a limited fashion, even that is new to me, I used to just just pass tons of individual objects to my methods rather than placing the objects inside classes and structs as I assume is much more common, so its all pretty new to me but structs more so. – Wobbles Feb 26 '15 at 11:53
  • @SriramSakthivel because I have no idea what that is or what you just said for starters lol, but I assume you want to know why I want to assign values directly to .X from an int, the simple answer is this will also be an interface for a plugin run application so I wanted to keep the interface fairly easy to use and streamlined. – Wobbles Feb 26 '15 at 11:56
  • So should I be using classes? I planned on wrapping all the control channels such as XYJoy inside a class called Device, but should the channels and control types themselves be classes? Im confused because I basically thought I would want to essentially create new types just like how string and bool are types (I think) but for control interface channels. – Wobbles Feb 26 '15 at 12:03
  • 1
    Yes you should be using classes. You come from a VB6 background don't you ? Learn about classes and a brand new world will open in front of your eyes :) – Franck Feb 26 '15 at 12:50
  • You already have the copy semantics, so just assign and modify. – leppie Feb 26 '15 at 12:52

0 Answers0