troubleshooting Question

Units Classes design

Avatar of deleyd
deleydFlag for United States of America asked on
C#
1 Comment1 Solution30 ViewsLast Modified:
I make a class Meters to represent meters, a class Feet to represent feet, and a class Length to represent a length.

Meters "is a" Length.
Feet "is a" Length.
So it sounds like Meters should inherit from Length, and Feet should inherit from Length.

I would also like Meters to have a UnitSymbol property, and Feet to have a UnitSymbol property.

But I can't have a UnitSymbol property for Length, because units are specific to the subtype.

But I do want every subtype to have a UnitSymbol property.

So I'll make UnitSymbol a virtual property in class Length, and have Meters and Feet overrride that.

Now I would like to be able to compare Feet to Feet, and Meters to Meters, using comparison operators <, <=, ==, !=, >=, >.

I can add that to Feet, and I can add that to Meters.

Can I add that to Length?

Now I have a problem comparing Feet to Meters.
Length m = new Meters(12);
Length f = new Feet(36);
if (f > m)
{
    ...
}

Open in new window

Well now it starts to get complicated.
What if I later add a class for Inches and a class for Kilometers? And I want to be able to compare any length with any other length?
Is this possible? Or am I going off in the wrong direction here?
ASKER CERTIFIED SOLUTION
Misha
Programmer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros