how to check the existence of an item in a list

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Hi

I have a text box that can be used for input and I would like to check if
the value in the text box that someone types is already in a list of items.
I know how to do this in other languages but not in C#.

I think i should create an array for the values already in the list - then
compare the input with each value in the array - but I dont know if this is
too inefficient.

Any advice appreciated.

Doug
 
Hi

I have a text box that can be used for input and I would like to check if
the value in the text box that someone types is already in a list of items.
I know how to do this in other languages but not in C#.

I think i should create an array for the values already in the list - then
compare the input with each value in the array - but I dont know if this is
too inefficient.

Any advice appreciated.

Doug
If the items are unique you could use a Dictionary<> to populate with
the items and then do a fast lookup with ContainsKey().
 
Doug said:
Hi

I have a text box that can be used for input and I would like to check if
the value in the text box that someone types is already in a list of
items. I know how to do this in other languages but not in C#.

I think i should create an array for the values already in the list - then
compare the input with each value in the array - but I dont know if this
is too inefficient.

Any advice appreciated.

You simply create a bool function that returns true or false, passing the
ctrl (the Listbox) and the search string as parms

You thee create a loop in the function using an idx starting from 0 to
ctrl.count.

In the loop, you make the comparsion between listbox.item.(idx).value,
something like that, to the search string, putting both values into upper
case.

If you get the hit, your return true; and leave the loop. Otherwise, you
return false; if the loop reaches the end.

You walk the listbox like an array.
 
Doug said:
Hi

I have a text box that can be used for input and I would like to check if
the value in the text box that someone types is already in a list of
items. I know how to do this in other languages but not in C#.

I think i should create an array for the values already in the list - then
compare the input with each value in the array - but I dont know if this
is too inefficient.

Any advice appreciated.

Doug
 
Oh, one other thing since you may not be using a listbox. :) You can pass
that array as a control to the function as well.
 
I have a text box that can be used for input and I would like to check if
the value in the text box that someone types is already in a list of
items. I know how to do this in other languages but not in C#.

I think i should create an array for the values already in the list -
then compare the input with each value in the array - but I dont know if
this is too inefficient.

How large is your list? For something reasonably small (even up to a few
hundred items, I'd say), a linear search is probably fine. You can either
just write your own foreach() loop, or use the Array.Find() generic method
(you have to provide a delegate to test for what you're looking for). The
latter is only really useful when you care more about finding the item and
less about where in the list it is (since you don't actually get an index
for the item back).

For larger lists, Ludwig's suggestion to use Dictionary<> is more
appropriate, or perhaps one of the other fast-lookup generic classes
(SortedDictionary<>, SortedList<>), depending on what you will do with the
results of the search.

Pete
 

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

Similar Threads


Back
Top