473,379 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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.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
6 2819
Curtis Rutland
3,256 Expert 2GB
I think Properties were designed for what you are doing. That's probably how I would do it.
Feb 2 '09 #2
Plater
7,872 Expert 4TB
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
229 Expert 100+
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
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
229 Expert 100+
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
7,872 Expert 4TB
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

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

Similar topics

5
by: Lars Plessmann | last post by:
I have a problem with typecast methods. Here is a setter of an object: function setKind($kind) { if (is_int((int)$kind) and (strlen($kind)<=6)) { $this->kind = $kind; return true;
2
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of typecast operator overloading. I am presenting...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
8
by: Xiaofeng Ye | last post by:
Sombody writes: HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL; why not: HASH_TABLE *hashed_filenames = NULL; thanks!
0
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it...
0
by: Martin Himmel | last post by:
Hello Newsgroup, I want to build xerces.lib (actually dynamical, but as there are many linker errors using the dll, the static .lib version) ich möchte die xercesc-lib erstellen. eigentlich die...
1
by: Martin Himmel | last post by:
Hello Newsgroup, I want to build xerces.lib (actually dynamical, but as there are many linker errors using the dll, the static .lib version) ich möchte die xercesc-lib erstellen. eigentlich die...
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
8
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.