【发布时间】:2019-12-04 10:04:51
【问题描述】:
所以我尝试使用 c# 中的内置“Array.BinarySearch”函数搜索和对象数组,但是,每当我搜索在文本框中输入的文本时,我都会得到“客户不存在”我实现的消息。
如果有人可以帮助我,那就太好了
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
int found = Array.BinarySearch(cust, tbCUSTOMERID.Text);
if (found == 0)
{
MessageBox.Show("Customer found!");
tbCUSTOMERID.Text = cust[found].GScID;
tbCUSTOMERNAME.Text = cust[found].GSname;
tbCITY.Text = cust[found].GSlocation;
tbEMAIL.Text = cust[found].GSemail;
}
MessageBox.Show("Customer doesn't exist");
}
如果您需要我的其他任何东西,请在 cmets 中找到 hmu
*EDIT: 数组已排序,“cust”数组是对象而不是字符串
【问题讨论】:
-
你认为
Array.BinarySearch是做什么的?显然cust[found] == tbCUSTOMERID.Text会返回false,因为cust是一个复杂对象,而不是string(我假设tbCUSTOMERID.Text是string)。 -
是的,你是对的,它是一个字符串,你知道解决这个问题吗?
-
@ZevSpitz 这有点无关紧要 - OP 抱怨无条件显示
MessageBox.Show("Customer doesn't exist");无条件显示......这有点......预期的行为? -
为避免询问 cmets 下一个问题,请重新阅读 minimal reproducible example 发布代码指南。看起来您的实际问题已经在 SO 上得到解答(即“按属性查找对象”或“按属性使用二进制搜索”),其中一个链接的重复项应该会给您答案。
标签: c# binary-search