Interface Problem

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
 
S

Stephan Rose

public static ScalarMillimeter operator + (ScalarMillimeter a, IScalar
b)
{
return a.Value*b.ToMillimeter().Value;

Correction: Should be

return new ScalarMillimeter(a.Value*(decimal)b.ToMillimeter().Value);
 
G

Guest

I think your structs must implement IComparable interface and then you will be able to make IScalar == IScalar and obtain the wished result , think about it


I hope this help...
 
S

Stephan Rose

I think your structs must implement IComparable interface and then you will be able to make IScalar == IScalar and obtain the wished result , think about it


I hope this help...

Unfortunately no luck :(

IScalar a = new ScalarMil(1000);
IScalar b = new ScalarMil(1000);

(a == b) = false still

Stephan
 
K

Kelly Leahy

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. :)

what do you mean you don't want them passed by reference? Interfaces are
passed as pointers, as are classes. Are you saying you don't want the ref
keyword to be applied ever? If that's the case, then you're going to have
problems with interfaces as well (it can be applied to them just as it can
to classes, it's just harder to do a "new" to find the thing to set them to,
but if you do a = b, then a refers to b, when a and b are interfaces - it
does not do a copy).
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.

you're going to need something like that operation anyway. Every time you
want to add two values together, they'll have to be converted to some common
unit, added, and then converted back to whatever unit you want the result
in. This is probably going to be just as "ugly" and big as the other
alternative.
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.

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? :)

I see no reason why you would need generics. You need value types, in my
opinion, and you just need to implement your interface on value types.
 
S

Stephan Rose

what do you mean you don't want them passed by reference? Interfaces are
passed as pointers, as are classes. Are you saying you don't want the ref
keyword to be applied ever? If that's the case, then you're going to have
problems with interfaces as well (it can be applied to them just as it can
to classes, it's just harder to do a "new" to find the thing to set them to,
but if you do a = b, then a refers to b, when a and b are interfaces - it
does not do a copy).

Yea....I've discovered that actually..*sigh* was hoping otherwise. :)
you're going to need something like that operation anyway. Every time you
want to add two values together, they'll have to be converted to some common
unit, added, and then converted back to whatever unit you want the result
in. This is probably going to be just as "ugly" and big as the other
alternative.

Actually nah...the way it works right now..only 1 conversion (whatever
unit the left side is).
I see no reason why you would need generics. You need value types, in my
opinion, and you just need to implement your interface on value types.

That's what I'm trying to do..but I'm trying to somehow have a common
ground that doesn't need to know about what unit it is, etc. Hence why
I was trying to use the interface for that. But I guess that's where
my thinking was off. :)

Hmm..gonna have to sleep over this...the answer will come to me :)

Thanks,

Stephan
 
H

Hasani

I had a similar problem with interfaces and the == operator

url ->
http://groups.google.com/groups?hl=...+get+this%22+dotnet+csharp&btnG=Google+Search

you have 2 choices, 1 cast the interface to a class but this assumes you
know what class the interface really is, or you could use the C# is operator
and a bunch of if statements.
e.x.:

IScalar a;
Iscalar b;
((ScalarMillimeter)a) == ((ScalarMillimeter)b)

or your second choice is to override the Equals() method for every class
that implements IScalar. That way, you can do

e.x.:
//if Equals() is successfully overridden, it will return true.
IScalar a = new ScalarMillimeter(10);
IScalar b = new ScalarCentimer(1);
a.Equals(b)

I think you should add an Add(IScalar rhs), Subtract(IScalar rhs),
Multiply(IScalar rhs), Divide(IScalar rhs) method to the IScalar interface
and either have it return a new IScalar that is the result of the operation
or have the invoked IScalar's value change.
Or, you could use your ToMillimeter() idea and have it return a value type
and use that value type's operator.

You can use structures, define a Meter, Centimeter, Millimeter, Decimeter,
Decameter, Hectometer and Kilometer. Have them all implement IScalar and
they will all return value types. Be careful of non-value types though
because the reference is copied. You can always implement Clone(), a copy
constructor, or both if this becomes a problem.

or use the equals
 
S

Stephan Rose

Thanks for the input Hasani.:)

most of what you said is precisely what I ended up implementing!

Stephan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top