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 7 1430
<snip> 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);
}
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...
On Fri, 16 Jan 2004 13:41:18 -0800, Hector Martinez
<an*******@discussions.microsoft.com> wrote:
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
> 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.
<snip> 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.
On Fri, 16 Jan 2004 17:13:41 -0600, "Kelly Leahy"
<ke********@nospam.swbell.net> wrote: 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).
Yea....I've discovered that actually..*sigh* was hoping otherwise. :) 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.
Actually nah...the way it works right now..only 1 conversion (whatever
unit the left side is). 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.
<snip>
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.
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
I had a similar problem with interfaces and the == operator
url -> http://groups.google.com/groups?hl=e...oogle%2BSearch
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
"Stephan Rose" <ke**********@kermos-no-spam-reversed.net> wrote in message
news:10********************************@4ax.com... 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
Thanks for the input Hasani.:)
most of what you said is precisely what I ended up implementing!
Stephan
On Sat, 17 Jan 2004 18:12:44 GMT, "Hasani" <HJ****@hotmail.c0m> wrote: I had a similar problem with interfaces and the == operator
url -> http://groups.google.com/groups?hl=e...oogle%2BSearch
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 "Stephan Rose" <ke**********@kermos-no-spam-reversed.net> wrote in message news:10********************************@4ax.com.. . 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Davide M3xican Coppola |
last post by:
Hi, I would realize an interface (interface) for a pair of concrete
classes (concrete & concrete2), but I have a problem because I have to
access to a third class (element) and if I try to declare...
|
by: Pol Bawin |
last post by:
A class has a private field of type IWizard (An interface) and a public
property to access it.
When I try to serialize the Geometry class in XML, i have an error but it
works in Binary
Can...
|
by: Ole Hanson |
last post by:
I am accessing my database through an interface, to allow future
substitution of the physical datastore - hence I would like to declare in my
Interface that my DAL-objects implementing the...
|
by: jon |
last post by:
How can I call a base interface method?
class ThirdPartyClass :IDisposable { //I can not modify this class
void IDisposable.Dispose() {
Console.WriteLine( "ThirdPartyClass Dispose" );
}
}
...
|
by: LS |
last post by:
Can a WebMethod return an Interface type?
Can we pass an interface parameter ?
Example :
public interface IEntity
{
long Id { get; set; }
string Name { get; set; }
}
|
by: masoud bayan |
last post by:
I've come across something in Interface implementation that I am not sure is
correct behavior in VB.NET (and maybe C#) or not?
Consider following example:
Public Interface IShape
|
by: stefan.moser |
last post by:
Hi All,
I'm having a problem implementing the Separated Interface pattern from
Martin Fowler's book Patterns of Enterprise Application Architecture.
I'm using C# in Visual Studio 2005.
The...
|
by: YellowFin Announcements |
last post by:
Introduction
Usability and relevance have been identified as the major factors
preventing mass adoption of
Business Intelligence applications. What we have today are traditional
BI tools that...
|
by: WTH |
last post by:
I am now aware (I am primarily a C++ developer) that in C# if you reference
the same interface from the same file in two different projects the types
are actually incompatible.
I found this out...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |