其他声明只会被执行 [英] Else statement only getting executed

查看:93
本文介绍了其他声明只会被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (LblMN.Text == "Incident")
          {

              if (LblURS.Equals("New"))
              {
                 LnkMSid.ToolTip="Incident is New";

                  GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/flag_blue.gif)  no-repeat;");
              }
              else if (LblURS.Equals("Resolved"))
              {
                  LnkMSid.ToolTip="Incident has been Resolved";
                  GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/flag_orange.gif)  no-repeat;");
              }
              else if (LblURS.Equals("Closed"))
              {
                  LnkMSid.ToolTip="Incident is Closed";
                  GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/flag_green.gif) no-repeat;");
              }
              else
              {
                  LnkMSid.ToolTip="Incident is under progress";
                  GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/flag_red.gif) no-repeat;");
              }
          }







以上是代码你能说出什么是错了..它执行,但直接转到Else声明是否条件真或假



并且如果staement那么留下以上所有......




Above is the code Can you tell what is wrong with it .. it execute, but straight goes to Else Statement whether the conditon true or false

and leave all the above else if staement .....

推荐答案

查看你的代码,这并不明显,但使用Equals方法意味着它不能用 == 运算符:

Looking at your code, it's not obvious, but the use of the Equals method implies it wouldn't compile with the == operator:
if (LblURS =="New")

大概给出了编译错误。

随着您对LblMN的引用遵循与LblURS相同的命名约定,我怀疑编译错误使您更改==以使用等于 - 这将在两个对象之间进行参考比较并且不太可能成功除非有直接转让或绝对转让给LblURS带来新感。



但是我怀疑没有,因为LblURS不是字符串 - 它是一个Label控件,就像LblMN一样。

所以改变您的代码返回==并改为使用Label.Text属性:

Presumably gave a compilation error.
With your reference to LblMN following teh same naming convention as LblURS, I would suspect that the compilation error made you change the "==" to use "Equals" - which will do a reference comparison between the two objects and is unlikely to succeed too well unless there is a direct assignment or the absolute string "New" to LblURS.

But I suspect there isn't, because LblURS is not a string - it's a Label control, just like LblMN.
So change your code back to "==" and use the Label.Text property instead:

if (LblMN.Text == "Incident")
{

    if (LblURS.Text == "New")
    {
       LnkMSid.ToolTip="Incident is New";

        GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/flag_blue.gif)  no-repeat;");
    }
    else if (LblURS.Text == "Resolved")
    {
        LnkMSid.ToolTip="Incident has been Resolved";
        GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/flag_orange.gif)  no-repeat;");
    }
    else if (LblURS.Text == "Closed")
    {
        LnkMSid.ToolTip="Incident is Closed";
        GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/flag_green.gif) no-repeat;");
    }
    else
    {
        LnkMSid.ToolTip="Incident is under progress";
        GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/flag_red.gif) no-repeat;");
    }
}







OP回复大量代码





哇!你不相信模块化代码......:笑:

让我们先把它设计得更具可读性,然后将它模块化并看看它会起什么作用。

创建一个switch语句来处理LblMN.Text值:




"OP Response with lots of code"


Wow! You don't believe in modular code then...:laugh:
Let's start off by making this a bit more readable, by modularising it and seeing what does what to what.
Create a switch statement to handle the LblMN.Text value:

KeyValuePair<string, string> kvp = new KeyVValuePair("???", "???");
switch(LblMN.Text.ToLower())
   {
   case "incident":
      kvp = HandleIncident(LblURS.Text);
      break;
   case "problem":
      kvp = HandleProblem(LblURS.Text);
      break;
   ...
   default:
      throw new ApplicationException("Unknown MN code: " + LblMN.Text);
   }
LnkMSid.ToolTip = kvp.Key;
GridView1.Rows[i].Cells[4].Attributes.Add("Style", "background: url(../Images/" + kvp.Value + ") no-repeat;"); 



每个方法都保存(基本上)每个if语句中的代码,并返回一个新的KeyValue对:


Each of the methods holds (basically) the code from inside each of your if statements, and returns a new KeyValue Pair:

private KeyValuePair<string, string> HandleIncident(string urs)
   {
   ...
   return new KeyValuePair<string, string>("Incident has been Resolved", "flag_orange.gif");
   ...
   }

这样,你的循环变得更加清晰,我们可以一目了然地看到它发生了什么。



(通常情况下,我会创建一个类来返回这对值,如果你这样做会是一个好主意,但这样做会有效 - 这只是很多打字而且我不喜欢我想把这个问题与新课混淆。)

This way, your loop becomes a lot clearer, and we can see at a glance what it going on.

(Normally, I'd create a class to return the pair of values, and it would be a good idea if you did, but that'll work - it's just a lot of typing and I don't want to confuse the issue with new classes.)


更好用



Better use

if (LblURS.ToUpper.Equals("NEW"))<big></big>


尝试使用以下语法。希望它能解决你的问题。

Try to use following syntax. Hope it will solve your issues.
if (LblURS.Text.ToUpper().Equals("NEW"))
{
........................
..........
...........
..........
      {


这篇关于其他声明只会被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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