473,796 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface Problem

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:

ScalarMillimete r a = new ScalarMillimete r(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 ScalarMillimete r operator + (ScalarMillimet er a, IScalar
b)
{
return a.Value*b.ToMil limeter().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
Nov 15 '05 #1
7 1455

<snip>
public static ScalarMillimete r operator + (ScalarMillimet er a, IScalar
b)
{
return a.Value*b.ToMil limeter().Value ;
Correction: Should be

return new ScalarMillimete r(a.Value*(deci mal)b.ToMillime ter().Value);
}

Nov 15 '05 #2


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...
Nov 15 '05 #3
On Fri, 16 Jan 2004 13:41:18 -0800, Hector Martinez
<an*******@disc ussions.microso ft.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
Nov 15 '05 #4
> 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.
Nov 15 '05 #5
On Fri, 16 Jan 2004 17:13:41 -0600, "Kelly Leahy"
<ke********@nos pam.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
Nov 15 '05 #6
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;
((ScalarMillime ter)a) == ((ScalarMillime ter)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 ScalarMillimete r(10);
IScalar b = new ScalarCentimer( 1);
a.Equals(b)

I think you should add an Add(IScalar rhs), Subtract(IScala r rhs),
Multiply(IScala r 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**********@k ermos-no-spam-reversed.net> wrote in message
news:10******** *************** *********@4ax.c om...
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:

ScalarMillimete r a = new ScalarMillimete r(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 ScalarMillimete r operator + (ScalarMillimet er a, IScalar
b)
{
return a.Value*b.ToMil limeter().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

Nov 15 '05 #7
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;
((ScalarMillim eter)a) == ((ScalarMillime ter)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 ScalarMillimete r(10);
IScalar b = new ScalarCentimer( 1);
a.Equals(b)

I think you should add an Add(IScalar rhs), Subtract(IScala r rhs),
Multiply(IScal ar 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**********@k ermos-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:

ScalarMillimete r a = new ScalarMillimete r(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 ScalarMillimete r operator + (ScalarMillimet er a, IScalar
b)
{
return a.Value*b.ToMil limeter().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


Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3371
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 interface class as friend, I obtain this (right) error: main2.cpp: In member function `virtual void concrete::method(element&)': main2.cpp:8: error: `int element::a_int' is protected main2.cpp:31: error: within this context
3
2309
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 you help me public class Geometry {
20
4263
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 interface and accessing the datastore MUST pass in a UserToken in the constructor of the object. Is this not possible? Am I forced to add the UserToken as a property on the object instead? /Ole
15
12796
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" ); } } class MyClass :ThirdPartyClass, IDisposable { void IDisposable.Dispose() {
5
10378
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; } }
12
1306
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
2
3370
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 problem I'm trying to solve is one of validation. Let's say I'm building a book store application and I have a Genre object in my Domain Model. When I'm adding a new genre, I want to validate the new object to make sure that a genre with the same...
0
2512
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 don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
7
10600
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 because I have written a generic plugin system for my current and future C# needs. I defined a base plugin system interface named IPlugin (original, I know...) which contains some basic plugin infomration all plugins of this system must expose...
0
9685
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.