473,405 Members | 2,160 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,405 software developers and data experts.

this can be fun...

i've abstract class Weight

public abstract class Weight
{
public int Quantity; // how much it weigths
}

and then i created 3 derived classes: Kilogram, Gram and Milligram
in each of them i created an implicit operator to convert from one to
another...

it works fine...

then in each of them is 3 constructors, one takes "Quantity" from int value,
and two others from other clsses, like for kilogram it would be milligram
and gram.

then i tryed to overload operator + for each of them...
and here troubles begin...
C# can't choose wich operator overload to use +, implicit, constructor...
a mess, i should say...
how this can be solved

thnx...
Nov 16 '05 #1
2 1446
Do like the framework handles dates and times. Create a class that can
accept weights in any measure, but represents it internally in a single unit
(like grams). Data can be converted on the way in from other measures to
the common unit, and converted back out depending on a format string or a
method call.

One class.

--- Nick

"David" <da*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
i've abstract class Weight

public abstract class Weight
{
public int Quantity; // how much it weigths
}

and then i created 3 derived classes: Kilogram, Gram and Milligram
in each of them i created an implicit operator to convert from one to
another...

it works fine...

then in each of them is 3 constructors, one takes "Quantity" from int value, and two others from other clsses, like for kilogram it would be milligram
and gram.

then i tryed to overload operator + for each of them...
and here troubles begin...
C# can't choose wich operator overload to use +, implicit, constructor...
a mess, i should say...
how this can be solved

thnx...

Nov 16 '05 #2
This sounds like pretty much complexity for the problem domain: If I got you
right, you got the conversion code 6 times (Kg -> g, Kg -> mg, g -> Kg, g ->
mg, mg -> Kg, mg -> g) in 3 different classes. Adding more metrics (like
pounds, etc) would result in an explosion of conversion functions. (Not even
mentioning addition, subtraction...)

I'd suggest something simple like:
public abstract class Weight
{
static double Kilogram(double kg) { return kg * 1000; }
static double Gram(double g) { return g; }
static double Milligram(double mg) { return mg / 1000; }
}
Thus keeping all internal data in one fixed format.

If you want to add type-safety (e.g. to prevent adding weights to lengths),
you could use a value-type like:
public struct Weight
{
private double val;
private Weight(double v) { val = v; }

public static Weight Kilogram(double kg) { return new Weight(kg * 1000); }
public static Weight Gram(double g) { return new Weight(g); }
public static Weight Milligram(double mg) { return new Weight(mg /
1000); }

// You could also declare the accessor functions as properties, if you
name them differently
public double Kilogram() { return val/1000; }
public double Gram() { return val; }
public double Milligram() { return val*1000; }

public static Weight operator + (Weight l, Weight r)
{ return new Weight(l.val + r.val); }

// other math operations
// ToString override, for kg/g/mg formatting
// ...
}

So you could write:
Weight x = Weight.Kilogram(5)+Weight.Gram(500);
double y = x.Milligram();

However, you should make performance tests to decide whether the added type
safety is worth the performance penalty.

Niki

"David" <da*******@hotmail.com> wrote in
news:%2****************@TK2MSFTNGP09.phx.gbl...
i've abstract class Weight

public abstract class Weight
{
public int Quantity; // how much it weigths
}

and then i created 3 derived classes: Kilogram, Gram and Milligram
in each of them i created an implicit operator to convert from one to
another...

it works fine...

then in each of them is 3 constructors, one takes "Quantity" from int value, and two others from other clsses, like for kilogram it would be milligram
and gram.

then i tryed to overload operator + for each of them...
and here troubles begin...
C# can't choose wich operator overload to use +, implicit, constructor...
a mess, i should say...
how this can be solved

thnx...

Nov 16 '05 #3

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

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
5
by: Scott D | last post by:
I am trying to check and see if a field is posted or not, if not posted then assign $location which is a session variable to $location_other. If it is posted then just assign it to...
2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: Alexander Ross | last post by:
I have a variable ($x) that can have 50 different (string) values. I want to check for 7 of those values and do something based on it ... as I see it I have 2 options: 1) if (($x=="one") ||...
0
by: Dan Foley | last post by:
This script runs fine, but I'd like to know why it's so slow.. Thanks for any help out there on how i can make it faster (it might take up to 5 min to write these 3 export files whith 15 records...
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: 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
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...
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...
0
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,...
0
jinu1996
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...
0
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...
0
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...
0
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,...

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.