Typecast operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fbooyens
    New Member
    • Feb 2009
    • 2

    Typecast operator

    I'm new to C# and come from an embedded C background so I hope I'm not embarassing myself not too much. I need to write a simulator for a variable frequency drive which sends device parameters to a PLC. What I would like to do is create a parameter class that contains information such as scaling factor, engineering value, binary value of the parameter etc so that a floating point parameter can be converted to binary for transmission.

    I'd like to instantiate the object as follows:
    VFDRealParam phasecurrent = new VFDRealParam(0, 1.25, VFDRealParam.pa ram_t.PARAM_BYT E);

    but when assigning to the parameter it would be great if it could be possible to simply do an assignment:
    phasecurrent = 67.4;

    I'm thinking of a class declaration such as:
    Code:
    class VFDRealParam : VFDParamBase
    {
        double _eng_data;
        double _scaling_factor;
        byte _byte_val;
        param_t _type;
    
        public VFDRealParam(double init_val, double scaling_factor, param_t type)
        {
            _eng_data = init_val;
            _scaling_factor = scaling_factor;
            _type = type;
            ScaleFromEng();
        }
    
        public static implicit operator VFDRealParam(double value)
        {
            ....
        }
    
        ....
    }
    When invoking the overloaded typecast operator VFDRealParam(do uble), it seems I can only return a new object of type VFDRealParam meaning that the scaling factor, default value etc. that was stored when the object was first instantiated is now lost. If it is actually possible, how can I go about doing this simple assignment, or do I have to resort to properties and using get/set? How can I get the parameter to retain its stored properties?

    Help from anybody will be greatly appreciated.
    Thanks!
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    I think Properties were designed for what you are doing. That's probably how I would do it.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Perhaps more like this:
      [code=c#]

      class VFDRealParam : VFDParamBase
      {
      double _eng_data;
      double _scaling_factor ;
      byte _byte_val;
      param_t _type;

      public VFDRealParam(do uble init_val, double scaling_factor, param_t type)
      {
      _eng_data = init_val;
      _scaling_factor = scaling_factor;
      _type = type;
      ScaleFromEng();
      }

      public double InitValue
      {
      get
      {
      return _eng_data;
      }
      set
      {
      _eng_data=value ;
      }
      }

      //....
      }

      [/code]

      Then to use it:
      [code=c#]

      VFDRealParam phasecurrent = new VFDRealParam(0, 1.25, VFDRealParam.pa ram_t.PARAM_BYT E);

      phasecount.Init Value= 0.1;
      [/code]

      Comment

      • vekipeki
        Recognized Expert New Member
        • Nov 2007
        • 229

        #4
        If you want to do an implicit conversion from Double to VFDParamBase, that can only be done by creating a new VFDParamBase instance according to the specified Double number - implicit conversion means that you should know how to create you entire class based on that single number (like a one-to-one relationship).

        You can, however, use multiple constructors to set some default values:

        Code:
        /// <summary>
        /// Creates a new instance of VFDRealParam, using init_val
        /// and default type/scaling factor.
        /// </summary>
        public VFDRealParam(double init_val) 
            : this(init_val, 1.25, VFDRealParam.param_t.PARAM_BYTE) { }
        
        /// <summary>
        /// Creates a new instance of VFDRealParam, using init_val
        /// and specified type/scaling factor.
        /// </summary>
        public VFDRealParam(double init_val, double scaling_factor, param_t type) 
        { 
            _eng_data = init_val; 
            _scaling_factor = scaling_factor; 
            _type = type; 
            ScaleFromEng(); 
        }
        And then, if it makes sense, add the implicit conversion also:

        Code:
        public static implicit operator VFDRealParam(double value)
        {
             return new VFDRealParam(value);
        }

        Comment

        • fbooyens
          New Member
          • Feb 2009
          • 2

          #5
          Thanks for the help so far. Actually, what I wanted to achieve is to do the implicit conversion using the object that has already been created. My intention was to create a variable with the scaling factor etc. and thereafter only do an assignment whenever the value changes without having to enter scaling factors again. The problem is that I would like the object to retain certain of its stored properties, but should you return a new object these will then be lost.

          If I create the variable using
          var = new foo(init, scaling, type) then I would like the variable to keep the values stored in scaling and type when doing an assignment such as
          var = 19.5.

          If such an implicit conversion can be done, great, else I'll have to stick to properties.

          Thanks!

          Comment

          • vekipeki
            Recognized Expert New Member
            • Nov 2007
            • 229

            #6
            No, not possible - the basic nature of the assignment operator is that it makes your variable point to a different object (when talking about reference types).

            If you want only one of the object's properties to point to a different object, then you cannot assign something to the object itself. If this were possible, it would be very confusing.

            Use an implicit operator only when it makes sense to create a new object based on a different type.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Are you saying you would like to keep the Scalling and Type values the same and just keep changing the "init value"
              What was wrong with using the Property then?

              Comment

              Working...