2

When I use a hard coded "4" in the second if, it works. But I have a dynamic string[] ProfileArray and want to check, if the value of View08ListBox1Item contains/not contains with one of the strings in ProfileArray. Why it does not work, when I change the "4" against the string[] ProfileArray?

global:

static string[] ProfileArray;


case "Profile":
            foreach (ListItem View08ListBox1Item in View08ListBox1.Items)
            {
                if (View08ListBox1Item.Selected)
                {
                    if (!View08ListBox1Item.Value.ToString().Contains("4"))
                    {
                        //:do something
                    }
                    else
                    {
                        //:do something
                    }
                }
            }
            break;

this was my first idea, but it does not work:

case "Profile":
            foreach (ListItem View08ListBox1Item in View08ListBox1.Items)
            {
                if (View08ListBox1Item.Selected)
                {
                    if (!View08ListBox1Item.Value.ToString().Contains(ProfileArray))
                    {
                        //:do something
                    }
                    else
                    {
                        //:do something
                    }
                }
            }
            break;
2
  • A string cannot contain an array.. its the other way around. Commented Aug 14, 2013 at 15:29
  • Look at the datatype for the argument in Contains: it wants a string. An array of strings is not the same thing as a plain-old string. Commented Aug 14, 2013 at 15:33

4 Answers 4

6

you can use Linq

ProfileArray.Any(x => x == View08ListBoxItem.Value.ToString())  //Contains
!ProfileArray.Any(x => x == View08ListBoxItem.Value.ToString()) //doesn't contain

non linq extension

public static bool Contains<T>(this T[] array, T value) where T : class
{
   foreach(var s in array)
   {
      if(s == value)
      {
         return true;
      }

   }
  return false;
}
ProfileArray.Contains(View08ListBoxItem.Value.ToString());
Sign up to request clarification or add additional context in comments.

2 Comments

or ProfileArray.All(x=>x == View08ListBoxItem.Value.ToString()) // doesn't contain
@ManuelFischer - Added an extension method that you can use
2

Because ProfileArray is an array not a string.

ProfileArray.Any(x => x == View08ListBox1Item.Value.ToString())

I think this can work.

In .NET 2.0 you can use

Array.IndexOf(ProfileArray, View08ListBox1Item.Value.ToString()) == -1

see http://msdn.microsoft.com/en-us/library/eha9t187%28v=vs.80%29.aspx

3 Comments

For strings it might be better to explore the StringComparison enumerator x=>x.Equals(View08ListBox1Item.Value.ToString(), StringComparison.*). Better control over how you want to match, I find that not very often do I care about case in strings.
@kelton52 sure, but it depends a lot on the type of info
That's true, just wanted to toss out a nuget of info.
1

A string cannot contain an array.. its the other way around.

You could use non-linq way too ProfileArray.Contains(View08ListBox1Item.Value.ToString())

Comments

1

Something like this maybe?

    bool found = false;
    for ( int i=0; i < ProfileArray.Length; i++)
    {
        if (View08ListBox1.SelectedItem.Value.ToString().Contains(ProfileArray[i])
        {
            found = true;
            break;
        }
    }

No need to iterate the listbox either as shown.

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.