Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# property, is it possible to get around defining get without defining set (no backing variable)?

Tags:

c#

properties

Lets say you have a Class with 300 properties with no backing variables, each of those properties returns a decimal/double.

Example:

public decimal MathValue { get; set; }

Now you decided that each one of those values should be rounded.

I am looking for the simplest way to refactor this without having to rewrite all of those properties.

Something, of this equivalent that actually works :D:

public decimal MathValue { get {return Math.Round(MathValue);} set; }
like image 741
Andrew Avatar asked Dec 01 '09 23:12

Andrew


3 Answers

No. If you need any custom logic in either getter or setter, you cannot use auto-properties.

like image 133
Pavel Minaev Avatar answered Sep 28 '22 22:09

Pavel Minaev


You could create a new value type that pretends to be a decimal, but returns the rounded value. Something like this:

struct RoundedDecimal
{
    public decimal Value { get; private set; }

    public RoundedDecimal(decimal value) : this()
    {
        this.Value = value;
    }

    public static implicit operator decimal(RoundedDecimal d)
    {
        return Math.Round(d.Value);
    }
}

Each property in your class should be of type RoundedDecimal instead of decimal.

like image 31
Andy Avatar answered Sep 28 '22 21:09

Andy


Easiest way to refactor the code? Here's what I would do:

  1. Open Notepad++ (get it if you don't have it)
  2. Copy/paste all properties of the class into a blank text area.
  3. Place the cursor at the start of the first line: public decimal MathValue1 { get; set; }
  4. Start recording a macro (click the record button on the toolbar)
  5. hold down ctrl+right arrow (called "word right") 3 times to put the cursor at the beginning of the property name.
  6. do shift+ctrl+right arrow 1 time and do a Copy to put the name of the property in the clipboard
  7. word right 3 more times to put the cursor after the "get"
  8. delete the semi-colon after the get and start typing " { return Math.Round(_"
  9. do a Paste 10 type "); }"
  10. word right 2 more times to put the cursor after the "set"
  11. delete the semi-colon after the set and start typing " { _"
  12. do a Paste
  13. type " = value; }
  14. press the End key to get the cursor to the end of the line
  15. press the right arrow key to get the cursor to the beginning of the next line.
  16. press the stop button to end your macro (square button on the toolbar)
  17. Click the "Run a macro multiple times" button (a double-arrow icon on the toolbar) and say "Run until the end of file"
  18. Copy/paste the resulting text back into your class to replace the original property definitions.

Now you'll need to define a set of corresponding private variables that begin with an underscore but otherwise have the same names as the properties. Start with a fresh copy of the properties from your class and perform a similar set of steps as described above.

My assumption is that each line starts with 2 tabs and there are no empty lines between properties.

Rather than having each property call Math.Round, you may want to consider defining your own utility function that they all call so that if you need to change it again, you can just change it in one place.

like image 35
Dr. Wily's Apprentice Avatar answered Sep 28 '22 20:09

Dr. Wily's Apprentice