5

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;
    }
  } 
1
  • You need to understand boolean logic better - the if/else if/else construct you have does not make sense with the test condition you have. Commented Aug 13, 2010 at 8:14

6 Answers 6

10

Change:

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

to

else if (!activationids.Contains(gvselectActID)) 
Sign up to request clarification or add additional context in comments.

Comments

6

Or even simpler

bool containsItem=activationids.Contains(gvselectActID);

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

1 Comment

+1: Whilst it doesn't specifically answer the question, it is the best approach to the sample code as a whole.
2

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.

Comments

2

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.

Comments

2

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]

1 Comment

if(x){doSomething();} else if(!x){doSomethingElse();} else {throw new LogicalBoundsOfUniverseExceededException();} (though strictly , if x is changed from the outside by another thread, the third condition could be hit).
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.