I'm trying to search an object array with text that has been typed into a text box.
I have converted the object array to a string array however, I'm still not having any luck with finding the correct index
I'm using the inbuilt binary search option within c# as that has been requested. I cannot change from this.
If anyone can help that would be great - If you need anything from me don't be afraid to drop me a comment.
Here's the customer array
Customer[] cust = new Customer[20];
Here's the sorting method for the customer class
private void customerSort()
{
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < customerPTR - 1; x++)
{
if (string.Compare(cust[x].GSname, cust[x + 1].GSname) > 0)
{
customerSwapRoutine(cust[x]);
}
}
}
}
and the swap routine
private void customerSwapRoutine(Customer book, int x = 0)
{
string tempString = cust[x].GSname;
cust[x].GSname = cust[x + 1].GSname;
cust[x + 1].GSname = tempString;
string tempString2 = cust[x].GScID;
cust[x].GScID = cust[x + 1].GScID;
cust[x + 1].GScID = tempString2;
string tempString3 = cust[x].GSlocation;
cust[x].GSlocation = cust[x + 1].GSlocation;
cust[x + 1].GSlocation = tempString3;
string tempString4 = cust[x].GSemail;
cust[x].GSemail = cust[x + 1].GSemail;
cust[x + 1].GSemail = tempString4;
}
Here's the customer class
class Customer
{
private string name, location, email, cID;
public string GScID
{
get { return cID; }
set { cID = value; }
}
public string GSname
{
get { return name; }
set { name = value; }
}
public string GSlocation
{
get { return location; }
set { location = value; }
}
public string GSemail
{
get { return email; }
set { email = value; }
}
public string displayCustomer()
{
return GScID + " " + GSname + " " + GSlocation + " " + GSemail;
}
}
Here's the search method
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
string[] str = new string[cust.Length];
for(int y = 0; y < cust.Length; y++)
{
if(cust[y] == null)
{
Customer nc = new Customer();
cust[y] = nc;
cust[y].GScID = "";
cust[y].GSemail = "";
cust[y].GSlocation = "";
cust[y].GSname = "";
}
str[y] = cust[y].GScID;
}
string stringcID = tbCUSTOMERID.Text;
int found = Array.BinarySearch(str, stringcID);
if (found < 0)
{
MessageBox.Show("Customer doesn't exist");
}
else
{
MessageBox.Show("Customer found!");
tbCUSTOMERID.Text = cust[found].GScID;
tbCUSTOMERNAME.Text = cust[found].GSname;
tbCITY.Text = cust[found].GSlocation;
tbEMAIL.Text = cust[found].GSemail;
}
}
xparameter, I have reason to believe this method will always look at the 0 and 1 elements of the array instead of moving through the array.