convert from int to byte
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.
Pl开发者_如何学运维ease help.
Thanks!
MYBO target=new MyBO();
target.x1=Convert.ToByte(20);
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)};
}
Have you tried explicitly casting to a byte?
MyBO target = new MyBO() { x1 = (byte) 20 };
精彩评论