如果情况有问题? [英] if condition problem?

查看:86
本文介绍了如果情况有问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if (TextBox1.Text ==  null 
{
Label1.Text = 输入有效值;
}
else {
sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode( @ // geometry / location / lat,XmlMngr1).InnerText;
sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode( @ // geometry / location / lng ,XmlMngr1).InnerText;

sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode( @ // geometry / location / lat ,XmlMngr2).InnerText;
sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode( @ // geometry / location / lng ,XmlMngr2).InnerText;

Label1.Text = sLong1 + + sLat1;
Label2.Text = sLong2 + + sLat2;


}





这个概念是

i want检查文本框值是否为空



i将这样的条件设为这样的



但是不工作...........





我保持文本框空值

它不会进入if条件



直接指向else代码并指向slong1值并给出错误说



对象引用未设置为对象的实例。

解决方案

您可以采取各种方法,



我个人会选择以下其中一项:



如果您使用的.Net框架版本较少比版本4

  if  String  .IsNullOrEmpty(TextBox1.Text.Trim())



否则请选择:

  if  String  .IsNullOrWhiteSpace(TextBox1.Text))



后一种方法不需要你首先修剪字符串,因为它会检测字符串是否只是空格,回车等空格字符。




 如果(TextBox1.Text ==  
{
Label1 .Text = 输入有效值;
}
else {
sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode( @ // geometry / location / lat,XmlMngr1).InnerText;
sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode( @ // geometry / location / lng ,XmlMngr1).InnerText;

sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode( @ // geometry / location / lat ,XmlMngr2).InnerText;
sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode( @ // geometry / location / lng ,XmlMngr2).InnerText;

Label1.Text = sLong1 + + sLat1;
Label2.Text = sLong2 + + sLat2;


}


如果你想检查是否只有空格那么你可以做



 if(TextBox1.Text.Trim()==)
{
Label1.Text =输入有效值;
}
else {
sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@// geometry / location / lat,XmlMngr1).InnerText;
sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@// geometry / location / lng,XmlMngr1).InnerText;

sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@// geometry / location / lat,XmlMngr2).InnerText;
sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@// geometry / location / lng,XmlMngr2).InnerText;

Label1.Text = sLong1 +,+ sLat1;
Label2.Text = sLong2 +,+ sLat2;


}


if (TextBox1.Text == null)
        {
        Label1.Text = "Input Valid values";
        }
        else {
            sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr1).InnerText;
            sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr1).InnerText;

            sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr2).InnerText;
            sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr2).InnerText;

            Label1.Text = sLong1 + ", " + sLat1;
            Label2.Text = sLong2 + ", " + sLat2;


        }



the concept of this is
i want to check if the textbox value is null or not

i put a if condition like this

but its not working...........


when i keep text box in null value
it will not go to the if condition

straightly point to the else code and point to the slong1 value and gives error saying

Object reference not set to an instance of an object.

解决方案

There are various approaches you can do to this,

personally I would go with one of the following:

If the .Net framework version you are using is less than version 4

if(String.IsNullOrEmpty(TextBox1.Text.Trim())


else go with:

if(String.IsNullOrWhiteSpace(TextBox1.Text ))


The latter approach does not require you to trim the string first as it will detect if the string is just whitespace characters such as space, carriage returns etc.


Try this..... :)

if (TextBox1.Text == "")
        {
        Label1.Text = "Input Valid values";
        }
        else {
            sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr1).InnerText;
            sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr1).InnerText;
 
            sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr2).InnerText;
            sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr2).InnerText;
 
            Label1.Text = sLong1 + ", " + sLat1;
            Label2.Text = sLong2 + ", " + sLat2;
 

        }


if you want to check if there is only white spaces then you can do

if (TextBox1.Text.Trim() == "")
        {
        Label1.Text = "Input Valid values";
        }
        else {
            sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr1).InnerText;
            sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr1).InnerText;
 
            sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr2).InnerText;
            sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr2).InnerText;
 
            Label1.Text = sLong1 + ", " + sLat1;
            Label2.Text = sLong2 + ", " + sLat2;
 
 
        }


这篇关于如果情况有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆