Help with .Contains

  • Thread starter Thread starter Kendall
  • Start date Start date
K

Kendall

Hi,

My form is - a listbox which contains 5 items, a textbox and a button.

ListBox - subjectListBox
TextBox - subjectTextBox


When the button is clicked, I want the 5 items in the listbox to be
compared to the text in the textbox (note: items in listbox are single
words - text in textbox can be many words) - and if any of the items in
the textbox match the an item in the list box, I want a message to
appear.

The error message I keep getting is: 'string' does not contain a
definition for 'Contains'

Here is the code I am trying to use within the button:

int counter;
int flag=0;

for (counter = 0; counter < 5; counter++){
if
(subjectTextBox.Text.Contains(subjectListBox.Items[counter].ToString()))
flag=1;
}
if (flag==1)
MessageBox.Show(strMessage);

I have emailed someone else who tested this code - and it worked for
them!
When this happend, I started a new project, and tried it again from
fresh - still not working.
Possibly a problem with my VSC#2003 insallation?

Any help would be greatly appreciated.
 
I have emailed someone else who tested this code - and it worked for them!

With all due respect... no it didn't. The compiler is telling you the
truth: the String type does not have a method called "Contains". You
probably want this instead:

boolean found=false;

for (counter = 0; counter < 5 && !found; counter++)
{
if
(subjectTextBox.Text.IndexOf(subjectListBox.Items[counter].ToString())
found=true;
}
if (found) MessageBox.Show(strMessage);
 
Bruce said:
With all due respect... no it didn't. The compiler is telling you the
truth: the String type does not have a method called "Contains". You
probably want this instead:
..net 2.0 (Visual Studio 2005) added a String.Contains method. It's
basically a neater way of saying String.IndexOf() >= 0.
 
Ahh. The evils of answering before asking which version the OP is
running. :-)

So, evidently the OP is running 1.1 (as I am) and the other person was
running 2.0.

Thanks, David. I learn something new every day here. :-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top