views:

119

answers:

4

I would like to specify that a decimal field for a price must be >= 0 but I don't really want to impose a max value.

Here's what I have so far...I'm not sure what the correct way to do this is.

[Range(typeof(decimal), "0", "??"] public decimal Price { get; set; }
+2  A: 

I would put decimal.MaxValue.ToString() since this is the effective ceiling for the decmial type it is equivalent to not having an upper bound.

Dr Herbie
The problem is that is not a constant. You'll get this error: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
+1  A: 

How about something like this:

[Range(0.0, Double.MaxValue)]

That should do what you are looking for and you can avoid using strings.

Jacob
That works for double but what about decimal?
+1  A: 

If your working with prices I'm sure you can safely assume nothing will cost more than 1 quadrillion dollars. ;)

I'd use:

[Range(0.0, 1000000000000)]

Or if you really need it, just paste in the value of Decimal.MaxValue ( without the commas ): 79,228,162,514,264,337,593,543,950,335

Either one of these will work well if you're not from Zimbabwe.

jfar
A: 

Oh well, it seems there's no choice but to put in the max value manually. I was hoping there was some type of overload where you didn't need to specify one.

[Range(typeof(decimal), "0", "79228162514264337593543950335"] public decimal Price { get; set; }