views:

112

answers:

3

Hello, I got the error:

System.ArgumentException: Object must be of type Int32.

in this code:

MyBO target = new MyBO() { x1 = 20 };

In MyBO i have an attribute: public byte x1 {get; set;}

What's wrong? I tried with MyBO target = new MyBO() { x1 = (byte)20 }; but i got the same error. Please help.

Thanks!

A: 

Have you tried explicitly casting to a byte?


MyBO target = new MyBO() { x1 = (byte) 20 };

Marc Bollinger
OK, then the problem is: if i have a validation rule, like:[RangeValidator(0, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive, Ruleset="validate_x1")] public byte x1 { get; set; }If i have MyBO my1 = new MyBO() {x1 = 20}, i got that error. Why?
qwerty
@qwerty, omg! please, edit your question to include that details....
Rubens Farias
I edited the question.
qwerty
So can you tell me why i got that error when using RangeValidator in MyBO class?
qwerty
+2  A: 
MYBO target=new MyBO();
target.x1=Convert.ToByte(20);
Earlz
+2  A: 

Are you sure that error comes from that line? I run this code without problems:

class MyBO
{
    public byte x1 { get; set; }   
}

// ...
public static void Main(string[] args)
{
    MyBO my1 = new MyBO() {x1 = 20};
    MyBO my2 = new MyBO() {x1 = (byte)20};
    MyBO my3 = new MyBO() {x1 = Convert.ToByte(20)};
}
Rubens Farias
So what causes the error?
qwerty
So post your REAL code, as code above doesn't reproduce your error
Rubens Farias