开发者

if string item is not in string array

I have a string array. I need to display buttons based on if the selected item is in the array. I need to know how to tell the program if "(array.NOT Contains("string"))". Please can anybody help me?? Thanks in advance

My code:

    List<string> ac开发者_开发技巧tivationids = new List<string>();
    foreach (ModuleActivation moduleactivation in activationid)
         activationids.Add(moduleactivation.ActivationID);

    string gvselectActID = GridView1.SelectedRow.Cells[1].Text;

    if (activationids.Contains(gvselectActID))
    {
      activateInsert.Visible = true;
      activateUpdate.Visible = false;
      deactivate.Visible = true;
    }
    else if (activationids."NOT" Contains(gvselectActID))
    {
      activateInsert.Visible = false;
      activateUpdate.Visible = true;
      deactivate.Visible = false;
    }
    else
    {
     activateInsert.Visible = false;
     activateUpdate.Visible = false;
     deactivate.Visible = false;
    }
  } 


Change:

else if (activationids."NOT" Contains(gvselectActID)) 

to

else if (!activationids.Contains(gvselectActID)) 


Or even simpler

bool containsItem=activationids.Contains(gvselectActID);

activateInsert.Visible = containsItem;
activateUpdate.Visible = !containsItem;
deactivate.Visible = containsItem;


The ! means "NOT". So you have to place it in front of the expression you need to negate;

!activationids.Contains("blahblah");

However, it's quite clear that if activationids.Contains("blahblah") is false, you are gonna go into the second case. Also, currently, your third block (... else { ...) will never be hit.


There are two very straightforward ways to do this:

  1. Not the result of the bool function call:

    if(!activationids.Contains(gvselectActID))

  2. Check the result and compare it to false

    if(activationids.Contains(gvselectActID) == false)


However, you are checking if it contains it in the first if() clause, which means that the first else clause will be fired if it isn't contained. There is no need to check, and there is no way that the third else will ever be fired.


Contains returns true or false, sou you cannot have three branches, you can do just

if (activationids.Contains(gvselectActID)) // it does contain
  ...
else // it does not contain
  ...

there are no other possibilities

[joke]
well it could work in this case
http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx
[/joke]


This will be enough:

if (activationids.Contains(gvselectActID))
{
  // Goes here if condition is true
  activateInsert.Visible = true;
  activateUpdate.Visible = false;
  deactivate.Visible = true;
}
else
{
  // Goes here if condition is false
  activateInsert.Visible = false;
  activateUpdate.Visible = true;
  deactivate.Visible = false;
}

There are no other possible options - there can't be a third branch.

This makes no sense:

if(booleanCondition)
{}
else if (!booleanCondition)
{}
else
{}

As by definition, if the booleanCondition is false, the else branch will be taken - there is no need to test for it being false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜