Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 753111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:51:34+00:00 2026-05-14T14:51:34+00:00

I have a variation on a Quantity (Fowler) class that is designed to facilitate

  • 0

I have a variation on a Quantity (Fowler) class that is designed to facilitate conversion between units. The type is declared as:

public class QuantityConvertibleUnits<TFactory> 
    where TFactory : ConvertableUnitFactory, new() { ... }

In order to do math operations between dissimilar units, I convert the right hand side of the operation to the equivalent Quantity of whatever unit the left hand side is in, and do the math on the amount (which is a double) before creating a new Quantity. Inside the generic Quantity class, I have the following:

    protected static TQuantity _Add<TQuantity>(TQuantity lhs, TQuantity rhs)
       where TQuantity : QuantityConvertibleUnits<TFactory>, new()
    {
        var toUnit = lhs.ConvertableUnit;
        var equivalentRhs = _Convert<TQuantity>(rhs.Quantity, toUnit);
        var newAmount = lhs.Quantity.Amount + equivalentRhs.Quantity.Amount;
        return _Convert<TQuantity>(new Quantity(newAmount, toUnit.Unit), toUnit);
    }

    protected static TQuantity _Subtract<TQuantity>(TQuantity lhs, TQuantity rhs)
       where TQuantity : QuantityConvertibleUnits<TFactory>, new()
    {
        var toUnit = lhs.ConvertableUnit;
        var equivalentRhs = _Convert<TQuantity>(rhs.Quantity, toUnit);
        var newAmount = lhs.Quantity.Amount - equivalentRhs.Quantity.Amount;
        return _Convert<TQuantity>(new Quantity(newAmount, toUnit.Unit), toUnit);
    }

    ... same for multiply and also divide

I need to get the typing right for a concrete Quantity, so an example of an add op looks like:

        public static ImperialLengthQuantity operator +(ImperialLengthQuantity lhs, ImperialLengthQuantity rhs) { return _Add(lhs, rhs); }

The question is those verbose methods in the Quantity class. The only change between the code is the math operator (+, -, *, etc.) so it seems that there should be a way to refactor them into a common method, but I am just not seeing it.

How can I refactor that code?

Cheers,
Berryl

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:51:34+00:00Added an answer on May 14, 2026 at 2:51 pm

    You could write an

     protected static TQuantity _Operator<TQuantity>(TQuantity lhs, TQuantity rhs,
                                           Func<double, double, double> op) 
           where TQuantity : QuantityConvertibleUnits<TFactory>, new()
     {  
            var toUnit = lhs.ConvertableUnit;  
            var equivalentRhs = _Convert<TQuantity>(rhs.Quantity, toUnit);  
            var newAmount = op(lhs.Quantity.Amount,equivalentRhs.Quantity.Amount);  
            return _Convert<TQuantity>(new Quantity(newAmount, toUnit.Unit), toUnit);  
      }  
    

    and call this function in your _Add, etc. using (d1, d2) => d1 + d2 as an additional parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have approximately 100 SQL views that are a variation of this: select *
Sometimes I see and have used the following variation for a fast divide in
I have a homework task to prove whether or not a particular variation on
This is a slightly original variation on a common problem. I have a fairly
I have two validation groups: parent and child I have an add button that
I have some jquery code the creates an quantity text box. Like so <input
I have a top-level page called ReceiveItem . Within that page, I have a
i have an spring web application (on glassfish server) that use JDBC connection pool.
I have a JQUERY AJAX post function that will work if there is one
I have a couple of models like so class Bill < ActiveRecord::Base has_many :bill_items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.