S
Stephan Rose
Ok here is my scenario
I have an interface called IScalar which describes a one dimensional
number that has a certain unit of measurement.
This interface is used to create multiple structures, each structure
representing a certain unit of measurement.
So I currently end up with 4 structures each implementing the IScalar
interface.
One for centimeters, millimeters, inch, and mils.
Each structure has all conversion multipliers, operators, etc. defined
so that I could do this without any problems:
ScalarMillimeter a = new ScalarMillimeter(1);
ScalarCentimer b = new ScalarCentimer(1);
IScalar c = a + b; // C would now end up 11 millimeters.
Works beautiful...
Here's where problems come in...
I can't do IScalar + IScalar for instance because that operator..is
not defined. Next problem is that IScalar == IScalar compiles because
the object base version of the == operator is used. However, the
result of that operation obviously is not correct. If I had two
values, each say 10 mils in distance that comparison would still
result in false.
The reason I'm not wanting to use classes is because I don't want to
risk that any of these things ever get passed by reference. I walked
into that trap hard once before, wanna stay out of it now.
I have thought of using just 1 structure and having a variable in it
that describes the unit VS a property in an individual structure that
returns the unit type hardcoded. However, what I don't like about that
approach is that with every operation I need either some if or case
statements to determine what units I'm adding or subtracting from one
another.
With my current approach, I always *know* (with the exception of the
IScalar,IScalar case) one half..and can blindly call the other
variables proper conversion method without having any conditional
statements.
Example:
public static ScalarMillimeter operator + (ScalarMillimeter a, IScalar
b)
{
return a.Value*b.ToMillimeter().Value;
}
That...is why I love this approach.
All my structures are set up in
this manner, and it makes any math operations a breeze this way and
keeps my code nice and neat and no if/case statements required
anywhere to determine what unit to convert to in multi-unit
operations. And since I'm using decimal as my type that holds the
numeric data, precision shouldn't be a problem if I convert from a
smaller unit to a larger one.
The only case being a problem is IStatic, IStatic on both sides....
So what are my options here? Essentially, this is what I need.
1. A base unit that I can use everywhere in my code that does not need
to know what type of unit it actually is. The ability to math
operations on these, comparisons, etc.
2. Want to use a structure so that upon assignment operators, a copy
is created...not a reference!!! Or is there a way to do this with
classes?
3. I guess what I truly need are generics?
Thanks,
Stephan
I have an interface called IScalar which describes a one dimensional
number that has a certain unit of measurement.
This interface is used to create multiple structures, each structure
representing a certain unit of measurement.
So I currently end up with 4 structures each implementing the IScalar
interface.
One for centimeters, millimeters, inch, and mils.
Each structure has all conversion multipliers, operators, etc. defined
so that I could do this without any problems:
ScalarMillimeter a = new ScalarMillimeter(1);
ScalarCentimer b = new ScalarCentimer(1);
IScalar c = a + b; // C would now end up 11 millimeters.
Works beautiful...
Here's where problems come in...
I can't do IScalar + IScalar for instance because that operator..is
not defined. Next problem is that IScalar == IScalar compiles because
the object base version of the == operator is used. However, the
result of that operation obviously is not correct. If I had two
values, each say 10 mils in distance that comparison would still
result in false.
The reason I'm not wanting to use classes is because I don't want to
risk that any of these things ever get passed by reference. I walked
into that trap hard once before, wanna stay out of it now.

I have thought of using just 1 structure and having a variable in it
that describes the unit VS a property in an individual structure that
returns the unit type hardcoded. However, what I don't like about that
approach is that with every operation I need either some if or case
statements to determine what units I'm adding or subtracting from one
another.
With my current approach, I always *know* (with the exception of the
IScalar,IScalar case) one half..and can blindly call the other
variables proper conversion method without having any conditional
statements.
Example:
public static ScalarMillimeter operator + (ScalarMillimeter a, IScalar
b)
{
return a.Value*b.ToMillimeter().Value;
}
That...is why I love this approach.

this manner, and it makes any math operations a breeze this way and
keeps my code nice and neat and no if/case statements required
anywhere to determine what unit to convert to in multi-unit
operations. And since I'm using decimal as my type that holds the
numeric data, precision shouldn't be a problem if I convert from a
smaller unit to a larger one.
The only case being a problem is IStatic, IStatic on both sides....
So what are my options here? Essentially, this is what I need.
1. A base unit that I can use everywhere in my code that does not need
to know what type of unit it actually is. The ability to math
operations on these, comparisons, etc.
2. Want to use a structure so that upon assignment operators, a copy
is created...not a reference!!! Or is there a way to do this with
classes?
3. I guess what I truly need are generics?

Thanks,
Stephan