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.

Is your array sorted? Binary search only works when the array is sorted. The contents of "File.TXT" would have to be sorted for it to work.

Let's learn some ways to write code more efficiently. If your code is hastily edited and cut for the sake of an example or you can't use things you've never learned and such, you don't need to reply back explaining yourself.
...

const string file = "File.TXT";

            // don't declare variables and assign them with values,
            // only to overwrite those values later.  How can you
            // stand it?
            string first = cBoxFirst.Text;
            string last = cBoxSecond.Text;

            // your stream reading code looked good, but note
            // you have this function available:
            string[] array = File.ReadAllLines(file);

            // note that you could have used
            // string[] array = nList.ToArray();
            // in your previous code.

            // you probably would want to sort your array here...

            int fIndex = Array.BinarySearch(array, first);
            int lIndex = Array.BinarySearch(array, last);

Also, make sure you read the documentation (http://msdn.microsoft.com/en-us/library/y15ef976.aspx) and see where negative numbers might be returned -- like when the string is not found.

Let's learn some ways to write code more efficiently. If your code is hastily edited and cut for the sake of an example or you can't use things you've never learned and such, you don't need to reply back explaining yourself.
...

const string file = "File.TXT";

            // don't declare variables and assign them with values,
            // only to overwrite those values later.  How can you
            // stand it?
            string first = cBoxFirst.Text;
            string last = cBoxSecond.Text;

            // your stream reading code looked good, but note
            // you have this function available:
            string[] array = File.ReadAllLines(file);

            // note that you could have used
            // string[] array = nList.ToArray();
            // in your previous code.

            // you probably would want to sort your array here...

            int fIndex = Array.BinarySearch(array, first);
            int lIndex = Array.BinarySearch(array, last);

Also, make sure you read the documentation (http://msdn.microsoft.com/en-us/library/y15ef976.aspx) and see where negative numbers might be returned -- like when the string is not found.

ya i was just throwing something together w/o showing all of my logic, but i really appreciate you pointing out the "string[] array = nlist.toarray()". I totally should have known this was possible.

as for my solution, it was an error caused by the unsorted list, but for my uses I had to keep the list random. I ended up using this to locate results and return index values...

int c = 0;
            while ((flag1 != true) && (c != array.Length))
            {
                if ((pres[c]) == (first))
                {
                    firstIndex = c + 1;
                    flag1 = true;
                }

                c++;
            }

            int t = 0;
            while ((flag2 != true) && (t != array.Length))
            {
                if ((pres[t]) == (last))
                {
                    lastIndex = t + 1;
                    flag2 = true;
                }

                t++;
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.