By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

Typecast operator

P: 2
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.param_t.PARAM_BYTE);

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:
Expand|Select|Wrap|Line Numbers
  1. class VFDRealParam : VFDParamBase
  2. {
  3.     double _eng_data;
  4.     double _scaling_factor;
  5.     byte _byte_val;
  6.     param_t _type;
  7.  
  8.     public VFDRealParam(double init_val, double scaling_factor, param_t type)
  9.     {
  10.         _eng_data = init_val;
  11.         _scaling_factor = scaling_factor;
  12.         _type = type;
  13.         ScaleFromEng();
  14.     }
  15.  
  16.     public static implicit operator VFDRealParam(double value)
  17.     {
  18.         ....
  19.     }
  20.  
  21.     ....
  22. }
When invoking the overloaded typecast operator VFDRealParam(double), 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!
Feb 2 '09 #1
Share this Question
Share on Google+
6 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
I think Properties were designed for what you are doing. That's probably how I would do it.
Feb 2 '09 #2

Plater
Expert 5K+
P: 7,872
Perhaps more like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class VFDRealParam : VFDParamBase 
  3.     double _eng_data; 
  4.     double _scaling_factor; 
  5.     byte _byte_val; 
  6.     param_t _type; 
  7.  
  8.     public VFDRealParam(double init_val, double scaling_factor, param_t type) 
  9.     { 
  10.         _eng_data = init_val; 
  11.         _scaling_factor = scaling_factor; 
  12.         _type = type; 
  13.         ScaleFromEng(); 
  14.     } 
  15.  
  16.     public double InitValue
  17.     {
  18.        get
  19.        {
  20.           return _eng_data;
  21.        }
  22.        set
  23.        {
  24.           _eng_data=value;
  25.        }
  26.     }
  27.  
  28.     //.... 
  29.  
  30.  
Then to use it:
Expand|Select|Wrap|Line Numbers
  1.  
  2. VFDRealParam phasecurrent = new VFDRealParam(0, 1.25, VFDRealParam.param_t.PARAM_BYTE);
  3.  
  4. phasecount.InitValue= 0.1;
  5.  
Feb 2 '09 #3

vekipeki
Expert 100+
P: 229
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:

Expand|Select|Wrap|Line Numbers
  1. /// <summary>
  2. /// Creates a new instance of VFDRealParam, using init_val
  3. /// and default type/scaling factor.
  4. /// </summary>
  5. public VFDRealParam(double init_val) 
  6.     : this(init_val, 1.25, VFDRealParam.param_t.PARAM_BYTE) { }
  7.  
  8. /// <summary>
  9. /// Creates a new instance of VFDRealParam, using init_val
  10. /// and specified type/scaling factor.
  11. /// </summary>
  12. public VFDRealParam(double init_val, double scaling_factor, param_t type) 
  13.     _eng_data = init_val; 
  14.     _scaling_factor = scaling_factor; 
  15.     _type = type; 
  16.     ScaleFromEng(); 
  17. }
  18.  
  19.  
And then, if it makes sense, add the implicit conversion also:

Expand|Select|Wrap|Line Numbers
  1. public static implicit operator VFDRealParam(double value)
  2. {
  3.      return new VFDRealParam(value);
  4. }
Feb 3 '09 #4

P: 2
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!
Feb 3 '09 #5

vekipeki
Expert 100+
P: 229
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.
Feb 3 '09 #6

Plater
Expert 5K+
P: 7,872
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?
Feb 3 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.