对于Int32,值太大或太小。 [英] Value was either too large or too small for an Int32.
问题描述
我有一个表单,可以在文本框文本更改时添加和减去数字。如果我输入2181003000 - 899763000之类的数字,我会收到此错误,对于Int32,值太大或太小。。我该如何解决这个问题?
protected void TextBoxNPRNA_TextChanged( object sender,EventArgs e)
{
int a = Convert.ToInt32(TextBoxTUNA.Text);
int b = Convert.ToInt32(TextBoxETRNA.Text);
int c = Convert.ToInt32(TextBoxNPRNA.Text);
TextBoxTNA2.Text = Convert.ToString(a + b + c);
TextBoxTR.Focus();
}
为什么人们坚持这样做?
永远不要使用Convert来改变用户输入的类型:改用tryParse。这样当用户犯错 - 就像他们一直做的那样 - 你可以在它成为问题之前检测到它,然后告诉他们他们要做些什么来解决它:
int a;
if (!int.TryParse(TextBoxTUNA.Text, out a))
{
// 向用户报告问题
返回跨度>;
}
int
可以存储介于-2 ^ 32-1到2之间的值^ 32。要存储更大的内容,请使用long
数据类型,该数据类型可以存储在-2 ^ 64-1到2 ^ 64之间。
int a = Convert.ToInt32(TextBoxTUNA.Text);
上面的问题代码与TextBoxTUNA.Text值2181003000是
你不能存小的大号。
-2,147,483,648 .. 2,147,483,647是int数据类型的范围;所以你怎么能把2181003000存放在int !!
就像试着把1.5升放入1升罐中一样。
你可以做点什么喜欢
Int32 a = Convert.ToInt32(TextBoxTUNA.Text);
I have a form that adds and subtracts numbers on textbox text change. If I put in a number like 2181003000 - 899763000 I get this error, "Value was either too large or too small for an Int32.". How can I fix this?
protected void TextBoxNPRNA_TextChanged(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBoxTUNA.Text);
int b = Convert.ToInt32(TextBoxETRNA.Text);
int c = Convert.ToInt32(TextBoxNPRNA.Text);
TextBoxTNA2.Text = Convert.ToString(a + b + c);
TextBoxTR.Focus();
}
Why do people insist on doing this?
Never use Convert to change the type of user input: use TryParse instead. That way when the user makes a mistake - as they do all the time - you can detect it before it becomes a problem, and tell them exactly what they have to do to fix it:
int a; if (!int.TryParse(TextBoxTUNA.Text, out a)) { // Report problem to user return; }
Anint
can store values between -2^32-1 to 2^32. To store anything larger, uselong
data type which can store between -2^64-1 to 2^64.
int a = Convert.ToInt32(TextBoxTUNA.Text);
Problem in above code with TextBoxTUNA.Text value 2181003000 is
You can not store big no in small.
-2,147,483,648 .. 2,147,483,647 is the range of int datatype; so how can you store 2181003000 in int!!
Its like try to put 1.5 liter into 1 liter can.
You can do something like
Int32 a = Convert.ToInt32(TextBoxTUNA.Text);
这篇关于对于Int32,值太大或太小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!