Wednesday, February 9, 2011

MidpointRounding enumeration

Is there a way I can configure the MidPointRounding enumeration default setting in a config file (I.e. web.config or app.config) I have a considerable source code base, and I need to configure at the application scope how rounding will occur, whether used in Math.Round or decimal type rounding... I would like to do this in order to get consistent rounding results throughout the application without changing every line that works with a decimal type or uses Math.Round....

  • Enum.Parse() is your friend here

    MyEnum GetEnumValue(string enumString) {
      return (MyEnum)Enum.Parse(typeof(MyEnum),enumString);
    }
    

    Obviously you'd also need some error-checking on the string you're getting from you're config file in which case you might want to return a default.

    From rohancragg
  • Nope, can't be done.

  • You can play games with post-compile tools that alter the assembly to call your function instead of Math.Round. However, I would just bite the bullet and change the source code.

0 comments:

Post a Comment