Avatar of deleyd
deleydFlag for United States of America

asked on 

Units Classes design

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?
C#

Avatar of undefined
Last Comment
Misha
ASKER CERTIFIED SOLUTION
Avatar of Misha
Misha
Flag of Russian Federation image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo