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

  • Home
  • SEARCH
  • 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 5933975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:02:57+00:00 2026-05-22T15:02:57+00:00

I have a similar problem to the post Accessing a static property of a

  • 0

I have a similar problem to the post Accessing a static property of a child in a parent method. The preferred answer hints that the design of the classes is faulty and more information is needed to discuss the problem.

Here is the situation I want to discuss with you.

I want to implement some unit aware datatypes like length, mass, current, …
There should be an implicit cast to create the instances from a given string. As example “1.5 m” should give the same as “150 cm”, or “20 in” should be treated correctly.

To be able to convert between different units, I need quantity specific conversion constants.
My idea was to create an abstract base class with some static translation methods.
Those should use class specific statically defined dictionary to do their job.
So have a look at the example.

public class PhysicalQuantities
{
    protected static Dictionary<string, double> myConvertableUnits;

    public static double getConversionFactorToSI(String baseUnit_in)
    {
        return myConvertableUnits[baseUnit_in];
    }
}

public class Length : PhysicalQuantities
{
    protected static Dictionary<string, double> myConvertableUnits = new Dictionary<string, double>()
    {
        { "in", 0.0254 }, { "ft", 0.3048 }
    };
}

class Program
{
    static void Main(string[] args)
    {
        Length.getConversionFactorToSI("in");
    }
}

I think this gives a rather intuitive usage and keeps the code compact and quite readable and extendable. But of course I ran into the same problems the referenced post describes.

Now my question is: How can I avoid this problems by design?

  • 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-22T15:02:58+00:00Added an answer on May 22, 2026 at 3:02 pm

    I think this could be solved with generics to still look readable. Refined as per Slaks suggestion to fit the registration into the static constructor to make it thread safe per se.

    So if I am not mistaken:

    • thread safe (all work on dictionary in the static constructor)
    • syntax still easy to use and readable SIConversion<Length>.GetFactor() (1 char more)
    • code needed to implement on derived classes very boilerplate register(string,double); (actually shorter than your dictionary definition)

      interface ISIConversionSubscriber
      {
          void Register(Action<string, double> regitration);
      }
      
      static class SIConversion<T> where T : ISIConversionSubscriber, new()
      {
      
          private static Dictionary<string, double> myConvertableUnits = new Dictionary<string, double>();
      
          static SIConversion() {
              T subscriber = new T();
              subscriber.Register(registrationAction);
          }
      
          public static double GetFactor(string baseUnit)
          {
              return myConvertableUnits[baseUnit];
          }
      
          private static void registrationAction(string baseUnit, double value)
          {
              myConvertableUnits.Add(baseUnit, value);
          }
      
      }
      
      abstract class PhysicalQuantities : ISIConversionSubscriber
      {
          public abstract void Register(Action<string, double> register);
      }
      
      class Length : PhysicalQuantities
      {
          public override void Register(Action<string, double> register)
          {
              // for each derived type register the type specific values in this override
              register("in", 1);
          }
      }
      
      class Program
      {
          static void Main(string[] args)
          {
              Console.WriteLine(SIConversion<Length>.GetFactor("in"));
          }
      }
      

    Output: 1

    In case you wonder why I made PhysicalQuantities abstract: to avoid using it with SIConversion<PhysicalQuantities>.GetFactor() since we don’t have a conversion for a base class. You probably don’t need instances of a base class like this anyway – it is not a full representation of a quantity, so it will probably contain reusable methods only.

    Another suggestion would be to use an Enum for the baseUnit instead of a string. Since everybody is striving for type safety and crying foul on magic strings, it probably is a good path to follow :))

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

Sidebar

Related Questions

I have a similar problem to the one that is answered in this post
Similar to this question: link However I have already mastered that. My problem is
I have a very similar problem to the post located here: Telerik grid with
I have a similar problem to this post . I need to display up
There have been some post with my similar problem: How do I iterate over
I have a similar problem with this ( https://wordpress.stackexchange.com/questions/9593/custom-post-type-archive-with-pagination ) and can't really figure
I have similar problem to one discussed here , but with stronger practical usage.
I notice this thread: Fastish Python/Jython IPC , and I have a similar problem,
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem similar to the egg and the chicken. I have two

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.