468,458 Members | 1,873 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

.FindString implementation

Hello!

I have recently come across two very nice methods in ListBox control:
..FindString and .FindStringExact
it is very usefull for me, and I would like to use it in some other
situations.

Where could I find an implementation of thiese methods or other similar
solutions. I would like to use such methods without ListBox - only with a
simple
Collection like SortedList (key and value). I have created a simple
implementation myself but I'm not sure it's that quick.

SortedList searchList = new SortedList();

// [...] - fill the list
foreach (DictionaryEntry de in searchList) {

string strKey = (string) de.Key;

if (strKey.StartsWith(strSearched)) {

return de.Value; break;

} else {

if (strKey.CompareTo(strSearched) > 0) break;

}

}
Marcin Floryan
marcin at floryan dot neostrada dot pl
Nov 16 '05 #1
1 2598
Marcin,

This is not as fast as it could be. What I would do is get the keys,
and then perform a binary search on it. Basically, since the keys should be
ordered (it is a sorted list, right), you can basically use a binary search.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marcin Floryan" <marcin[REMOVE]@floryan.neostrada.pl> wrote in message
news:O%****************@TK2MSFTNGP15.phx.gbl...
Hello!

I have recently come across two very nice methods in ListBox control:
.FindString and .FindStringExact
it is very usefull for me, and I would like to use it in some other
situations.

Where could I find an implementation of thiese methods or other similar
solutions. I would like to use such methods without ListBox - only with a
simple
Collection like SortedList (key and value). I have created a simple
implementation myself but I'm not sure it's that quick.

SortedList searchList = new SortedList();

// [...] - fill the list
foreach (DictionaryEntry de in searchList) {

string strKey = (string) de.Key;

if (strKey.StartsWith(strSearched)) {

return de.Value; break;

} else {

if (strKey.CompareTo(strSearched) > 0) break;

}

}
Marcin Floryan
marcin at floryan dot neostrada dot pl

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.