Question

This is the property declaration in question:

 [RangeValidator(1,RangeBoundaryType.Inclusive,255,RangeBoundaryType.Inclusive,MessageTemplate = "StartFlexibility is out of range")]
    public byte StartFlexibility { get; set; }

When the validate method is called, a FormatException is thrown telling me that the value type needs to be Int32.

How to fix, please?

Was it helpful?

Solution

well... the quick obvious fix will be change the type to short or int,

but another observation i want to do, is with the range. You are telling the RangeValidator to take a inclusive range between 1 and 256, but you just can assign a byte value till 255, maybe that's the compiler reason to cry out.

The RangeValidator is also infering the type of the Range from the parameters, so, try casting

[RangeValidator((byte) 1, ...

OTHER TIPS

As Jhonny says, cast to byte... but more like this

[RangeValidator(typeof(Byte), "1", RangeBoundaryType.Inclusive, "255", RangeBoundaryType.Inclusive, MessageTemplate = "Some message")]

The other option would be to call the range validator in a SelfValidation message and cast there.

I've never used the RangeValidator class/attribute, but is it an issue that you have 256 as an upper bound when a byte can only get to 255?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top