System.ArgumentException: Object must be of type Int32.
从int转换成字节
在此代码:
MyBO target = new MyBO() { x1 = 20 };
在MyBO
我有一个属性:public byte x1 {get; set;}
有什么不对?我试过MyBO target = new MyBO() { x1 = (byte)20 };
但我得到了同样的错误。 请帮忙。
谢谢!
System.ArgumentException: Object must be of type Int32.
从int转换成字节
在此代码:
MyBO target = new MyBO() { x1 = 20 };
在MyBO
我有一个属性:public byte x1 {get; set;}
有什么不对?我试过MyBO target = new MyBO() { x1 = (byte)20 };
但我得到了同样的错误。 请帮忙。
谢谢!
你有没有尝试明确地转换成一个字节?
MyBO target = new MyBO() { x1 = (byte) 20 };
行,则问题是:如果我有一个验证规则,如: [RangeValidator控件(0,RangeBoundaryType.Inclusive,20%,RangeBoundaryType.Inclusive,规则集= “validate_x1”)] 公共字节X1 { GET; 集; } 如果我有MyBO my1 = new MyBO(){x1 = 20},我得到了那个错误。为什么? – qwerty 2010-01-28 18:21:40
@qwerty,omg!请编辑你的问题,以包括这些细节.... – 2010-01-28 18:22:47
我编辑了这个问题。 – qwerty 2010-01-28 18:23:45
MYBO target=new MyBO();
target.x1=Convert.ToByte(20);
你确定错误来自该行?我运行此代码没有问题:
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)};
}
那么是什么导致了错误? – qwerty 2010-01-28 18:36:22
因此,发布您的REAL代码,因为上面的代码不会重现您的错误 – 2010-01-28 19:06:07
这是什么语言?请添加到标签列表中... – Galghamon 2010-01-28 18:15:03
它似乎是C#,给定对象初始值设定项和'System.ArgumentException'。 – 2010-01-28 18:15:59
你确定这是错误发生的地方吗?我用类似的声明做了一个类似的类,没有任何问题。 – Aaron 2010-01-28 18:18:49