1
Answer

Using ArrayList.BinarySearch problems

Tom

Tom

16y
4.6k
1
Hi there, just some simple code not working very well, wondering if someone could point me in the right direction...
Arraylist Salesman = new Arraylist();

if (myArray[22].Length > 0)
            {
                myDataRow["SalesmanID"] = myArray[22];
                //Adding Salesman to the Salesman Arraylist
                Salesman.Sort();
                 int x = Salesman.BinarySearch(myArray[22]);
                               
                                if (x < 0)
                                {
                                    Salesman.Add(myArray[22]);
                                }
                                else
                                {

                                }
                            }
else
                   {
                                myDataRow["SalesmanID"] = 0;
                    }

I am running through this, Myarray[22] = 49 the first time it is run, so it adds 49 to theSalesman Arraylist, the next value of Myarray[22] is 49 as well, so it sees that it exists and doesn't add it. (perfect !).  All is well when the follwing go through 7,7,7,45, but then another 45 is passed through and instead of finding it in the arraylist, x becomes a negitive number (-1) and it is added to the arraylist... :(
I am simply trying to make a list of unique salesman as the data is coming through..
If anyone can help that would be fantastic!

Cheers,
Tom
Answers (1)