0

Here is what I have already :

    public class Human
    {
        [Mode(MaxValue = 1000)]
        public Var MoneyInPocket = new Var(); 

        [Mode(MaxValue = 75)]
        public Var Age= new Var(); 
    }

In my method :

        Human Eric = new Human();

        Eric.MoneyInPocket = 50;   //OK!
        Eric.Age= 200; // It's automatically changed to 75. So it's okay too!

Everything works fine other than I can't change MaxValue at run-time.

I don't want to put MaxValue as part of Human or Var classes and also I don't want to have a second class to store my variables other than class Var.

Mohsen Sarkar
  • 5,910
  • 7
  • 47
  • 86
  • 1
    Related: http://stackoverflow.com/questions/51269/change-attributes-parameter-at-runtime – Andris Jan 29 '14 at 07:48
  • @Andris Actually this is the solution not only related! I could accept this comment if your post it as answer – Mohsen Sarkar Jan 29 '14 at 08:40
  • Not really! :( It shows that each time `GetCustomAttributes` is called you get a new instance of your `ModeAttribute` class. You can change the properties of this instance but it will have no effect on the result of the following calls to `GetCustomAttributes`. The property in the objects returned by the new call will stay as what was set initially. This is probably not what you want. – Andris Jan 29 '14 at 09:23
  • @Andris Oh I didn't notice it. Thank you again! – Mohsen Sarkar Jan 29 '14 at 12:12

3 Answers3

1

You can't with an attribute. Attributes have to be compile-time constant.

You need to set it up as a property and have the setter check the value against your configured MaxValue.

public string Age
{
   get { return _age; }
   set 
   {
        if (value > MyConfiguration.MaxValue)
           _age = MyConfiguration.MaxValue;
        else
           _age = value;
    }
}

EDIT: Thanks to @Andris for posting a related question/answer: Change Attribute's parameter at runtime

Apparently it is possible if the attribute will play nicely with having its value changed using reflection. I never really thought about doing that before although I never really had to.

Community
  • 1
  • 1
TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • It's a acceptable solution but I don't want to repeat this code for all my 500 items inside my class. I feel a lot better if it's possible to do this in a one line solution! – Mohsen Sarkar Jan 29 '14 at 07:25
  • @Mahdi I understand. Unfortunately, without already having some sort of business object framework in place, you are kind of forced to either turn your fields into properties and do the validation yourself. I don't know what `Var` is, but if you are not in control of the code when it gets set, I don't see any other option. =/ – TyCobb Jan 29 '14 at 07:30
0

Your question is not complete.

You can use property instead field and use [Range(0, 100)].

0

You can wrap all your properties into a special type, which can support extra (min, max, default value, whatever). Or extend Var class to have it

class MyVar
{
    public Var Value;
    public Var Min;
    public Var Max;
}

Then you can initialize your properties in one line (use constructors overloads too)

public class Human
{
    public Var MoneyInPocket = new Var() { Max = 1000 }; 
    public Var Age = new Var() { Max = 75 }; 
}

Then you can change them in run-time, serialize/deserialize.

You could still use attributes to tell a default value for, to example, Max. It will be used if Max is missing (have default value). But as soon as Max is set to something during run-time, then you don't take value from attribute. But this scenario is a little bit too much, compared to pure property solution.

Sinatr
  • 20,892
  • 15
  • 90
  • 319