C# 无法比较int?无效
看看我用文本框的textchanged事件包围的代码:C# 无法比较int?无效,c#,visual-studio,C#,Visual Studio,看看我用文本框的textchanged事件包围的代码: string testString = comboIPAddress.Text; string[] parts = testString.Split('.'); int? ipclass = int.Parse(parts[0]); if (ipclass == null) { //do nothing }
string testString = comboIPAddress.Text;
string[] parts = testString.Split('.');
int? ipclass = int.Parse(parts[0]);
if (ipclass == null)
{
//do nothing
}
if (ipclass >= 1 && ipclass <= 126)
{
comboSubnet.Text = "255.0.0.0";
}
if (ipclass >= 192 && ipclass <= 223)
{
comboSubnet.Text = "255.255.255.0";
}
if (ipclass >= 128 && ipclass <= 191)
{
comboSubnet.Text = "255.255.0.0";
}
else
{
comboSubnet.Text = "";
}
string testString=comboIPAddress.Text;
string[]parts=testString.Split('.');
智力?ipclass=int.Parse(部分[0]);
如果(ipclass==null)
{
//无所事事
}
如果(ipclass>=1&&ipclass=192&&ipclass=128&&ipclass使用不可为空的int
,并检查是否可与
使用不可为空的int
并检查是否可与
在尝试拆分(!string.IsNullOrEmpty(testString))之前,应该测试是否(!string.IsNullOrEmpty(testString))
,然后测试是否(!string.IsNullOrEmpty(testString))
在尝试拆分之前,后跟int.TryParse
您是否考虑过为IP使用MaskedTextBox?然后您可以使用
如图所示。您是否考虑过为IP使用MaskedTextBox?然后您可以使用
如图所示。如果testString
为空,那么parts
的内容是什么?您应该阅读:如果testString
为空,那么parts
的内容是什么?您应该阅读:它只需要复制和粘贴。谢谢,它只需要复制和粘贴。谢谢
int ipclass;
if (!int.TryParse(parts[0], out ipclass))
{
//do nothing
}