Reputation: 287
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> activationids = 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;
}
}
Upvotes: 5
Views: 13449
Reputation: 933
Or even simpler
bool containsItem=activationids.Contains(gvselectActID);
activateInsert.Visible = containsItem;
activateUpdate.Visible = !containsItem;
deactivate.Visible = containsItem;
Upvotes: 6
Reputation: 1851
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]
Upvotes: 2
Reputation: 499062
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.
Upvotes: 1
Reputation: 46425
There are two very straightforward ways to do this:
Not the result of the bool
function call:
if(!activationids.Contains(gvselectActID))
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.
Upvotes: 2
Reputation: 48547
Change:
else if (activationids."NOT" Contains(gvselectActID))
to
else if (!activationids.Contains(gvselectActID))
Upvotes: 10
Reputation: 6694
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.
Upvotes: 2