I have an application I'm building that requires the use of array.binarysearch() to find strings from a combobox in an array and return the index value of the selection. Ie. the user selects a name from the combobox. I now need to use the name they chose and search an array for the same value, and return the index for further use in my application. The combobox is filled randomly so I can't just associate my combobox.selectedindex value.
Here is what I have so far...
...
const string file = "File.TXT";
List<string> nList = new List<string>();
string first = "";
string last = "";
first = cBoxFirst.Text;
last = cBoxSecond.Text;
using (StreamReader s = new StreamReader(file))
{
string line;
while ((line = s.ReadLine()) != null)
{
nList.Add(line);
}
}
string[] array = new string[nList.Count()];
int i = 0;
foreach (string s in nList)
{
array[i] = s;
i++;
}
int fIndex = Array.BinarySearch(array, first);
int lIndex = Array.BinarySearch(array, last);
This seems like it should work, but both of these return the same number each time, and it is never accurate and always negative. Please tell me what I'm overlooking.