views:

109

answers:

1

Hi All

I'd like to ask re: measurement conversion on the fly, here's the detail :

Requirement: To display unit measurement with consider setting. Concerns: - Only basic (neutral) unit measurement is going to be stored in database, and it is decided one time.

  • The grid control has direct binding to our business object therefore it has complexity to do conversion value.

Problem:

How to display different unit measurement (follow a setting), consider that controls are bind to business object?

Your kind assistance will be appreciated. Thank you

ikadewi

A: 

If I've understood your question correctly (you are a little vague...) you want to store measurement data in one way, but give the users the option to display it in different ways (using different units). You don't specify which technology/language environment you're using, but there is (at least) one pretty straightforward way to do this: create a converter class.

Here's some pseudo-C# skeleton code if your measurement data is lengths, stored in millimeters. You can probably figure out how to use the same approach for whatever you're measuring, and however you want to display it:

class LenghtConverter {

    double ToCentimeters(double millimeters) {
        // 1 centimeter = 10 millimeters
        return millimeters / 10;
    }

    double ToInches(double millimeters) {
        // 1 inch = 25.4 millimeters
        return millimeters / 25.4
    }

    // You get the drift. Add whatever conversions you need. If you wish, 
    // you can return strings instead of numbers, and append the unit
    // signature as well.
}

Now, in your grid you display your data with some kind of presentation syntax. I'm making something up to give you an idea, and since I'm into ASP.NET the syntax is pretty similar to that. Hope you'll excuse me for that =)

Instead of just

<%= MyMeasurement.Data %>

to display the measurement data in the way it was stored, you output with

<%= LenghtConverter.ToInches(MyMeasurement.Data) %>

which will display the result in inches.

If you're actually using C# (or VB.NET, I suppose) there is a nice feature available in .NET 3.5 called Extension Methods that you might want to use instead. That would let you output with the somewhat cooler and more streamlined syntax

<%= MyMeasurement.Data.ToInches() %>
Tomas Lycken