[英]Input string was not in correct format. Handle exception
我收到异常“输入字符串的格式不正确”。 我想处理该异常并添加我自己的错误。 输入应为int。 我应该在哪里做? 我有一个带listview的objectdatasource,并且在从后面的代码获取textbox.text时遇到麻烦,因此可以使用tryParse。
您的媒体资源为Int32类型。 您不能为该属性分配除有效整数以外的任何内容。 现在,如果您有一些字符串形式的用户输入,然后需要将其分配给integer属性,则可以使用int.TryParse方法来确保用户输入的值是有效的整数。
例如:
string someValueEnteredByUser = ...
int value;
if (!int.TryParse(someValueEnteredByUser, out value))
{
// the value entered by the user is not a valid integer
}
else
{
// the value is a valid integer => you can use the value variable here
}
Number
始终是一个int
,它的定义方式是...
您可能想要验证字符串的内容。 最简单的方法是将其解析为一个int
:
int number;
if(!int.TryParse(yourString, out number))
{
Not an int!
}
“值”将始终与变量属于同一类型。 因此具有:
private bool mabool = false;
public bool MaBool
{
get { return mabool; }
set { mabool = value; }
}
永远不会崩溃。 正如我所说,这是因为值将是变量的相同类型。 在这种情况下,value是布尔值。
尝试上课:
public class Rotator
{
public Roll, Pitch, Yaw;
// Declarations here (...)
}
private Rotator rotation = new Rotator();
public Rotator Rotation
{
get { return rotation; }
set
{
// Since value is of the same type as our variable (Rotator)
// then we can access it's components.
if (value.Yaw > 180) // Limit yaw to a maximum of 180°
value.Yaw = 180;
else if (value.Yaw < -180) // Limit yaw to a minimum of -180°
value.Yaw = -180;
rotation = value;
}
}
如第二个示例所示,value是Rotator,因此我们可以访问它的组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.