I am helping my friend develop a Unit Converter. He asked me if I could help split up the work. So I am making a .dll called Unit. Unit is supposed to handle the conversions. I was brainstorm on how to do this and I came up with an idea to put give each section an enum (like enum Angle[Degrees, Radians, Gradians], enum Area[Square meters, square miles, …], enum Energy[Newtons, Pascals, Joules, …], …). Then a main abstract class Unit with all the Convert methods
NOTE:
I am NOT asking you to code the .dll, I just want your opinion on HOW to make the .dll.
I am helping my friend develop a Unit Converter. He asked me if I
Share
Something to watch for when you have a many-to-many relationship like this (every value wants to be able to be converted to every other kind of value in its class) is that you’ll end up writing a ton of methods if you use the brute force method of writing a conversion function for each possibility.
This is OK if you need it to work as fast as possible and want to make sure your error is controlled, but you suffer for code maintainability.
Something I’ve done in the past for converting between many units was to establish a ‘base’ unit that everything could convert to and write functions to convert to/from that. This lets you define an interface something like:
And specialize that for certain types of conversion:
Edit: Usage:
This can be somewhat heavyweight and you have to watch for propagation of error hitting you for two conversions instead of one. It has come in handy when I needed to do complex GIS conversions that needed state stored as well, but it might not be what you’re looking for exactly.