Keeps returning a negative to the binarysearch

May 6 2008 4:33 PM

When I run the code, I make my second entry the same as the first, assuming the binarysearch finds the same entry in the array, I should get the message box.

The Problem is that the search dosen't find the same entry even though I have entered the same number. 

public partial class Form1 : Form

{

int[] storage = new int[20];

int counter = 0;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

//number is read from "entry"

int entry = Int32.Parse(Entry.Text);

int retVal = Array.BinarySearch(storage,entry);

if (retVal < 0)

// add to storage if not there

{

storage[counter] = entry;

counter++;

}

else // Message box

{

MessageBox.Show("That entry is already listed, Please enter a different number.",

"Need a new entry", MessageBoxButtons.OK);

}


Answers (1)