views:

74

answers:

2

Hello,

So, in a MyBO class i have:

[NotNullValidator(MessageTemplate = "Cannot be null!")]
    [RangeValidator(0, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive, Ruleset="validate_x1")]
    public byte x1
    {
        get;
        set;
    }

And in a test class:

        [TestMethod()]
    public void x1Test()
    {
        MyBO target = new MyBO() { x1 = (byte)20 };
        ValidationResults vr = Validation.Validate(target, "validate_x1");
        Assert.IsTrue(vr.IsValid);
    }

Why i got: Test method TestProject.CatedraBOTest.x1Test threw exception: System.ArgumentException: Object must be of type Int32.. ?

I really don't understand. If i remove the RangeValidator everything works fine. Please help.

A: 

Hi there.

I'm guessing that the RangeValidator attribute does not work on BYTE values?

If you look at the documentation it says:

The Range Validator can be used with any type that implements the IComparable interface

I don't believe that BYTE is one of those types?

EDIT: I stand corrected, the BYTE data type does implement IComparable (just checked in Reflector) so I'm not sure now what the issue is. I'll keep looking.....

EDIT: Found this which might help. Cheers. Jas.

Jason Evans
Ok, thanks. I don't know what causes the error..
qwerty
A: 

Adding to what @Jason said, look at this where none of the constructor accept byte as parameter for range validation.

Having said that, you could use this if you still want to compare it with byte. In that case, it could look like

[RangeValidator(typeof(Byte), "0", RangeBoundaryType.Inclusive, "20", RangeBoundaryType.Inclusive)]

Note that the above line is written based on what I could see & interpret of the documentation. I have not written code to test if this works.

This is just to give you an idea of how things might work.

EDIT: The alternative could be to change the type of the property from byte to int.

shahkalpesh
Worked, thanks! ;)
qwerty