Searching Arraylist not working
Below is a snippet of code i am trying to use to search an arraylist for. I have used code very similar to this in a different form and it has worked. However here it has not. It is meant to search for the Customer Number within the Arraylist on frmMain, it then searches for that customers Account Number typed in by the user. But for some reason it is almost as if it skips the second if statement and go to the error message at the bottom.
The Arraylist is on the frmMain, then there are classes for Customer Account and Transaction. In Customer there is another ArrayList storing the Accounts, then in the Account Class there is an ArrayList storing the transactions of the customer account.
foreach (Customer a in frmMain.bankDetails)
{
if (a.getCustomerNumber().ToUpper().Equals(custSearch))
{
foreach (Account b in a.Ac开发者_如何学运维counts)
{
if (b.getAccNumber().Equals(searchString))
{
txtSearch.BackColor = Color.PaleGreen;
txtAccSortCode.Text = b.getAccSort();
txtAccNumber.Text = Convert.ToString(b.getAccNumber());
txtAccNickName.Text = b.getAccNick();
txtAccDate.Text = b.getAccDate();
txtAccCurBal.Text = Convert.ToString(b.getAccCurBal());
txtAccOverDraft.Text = Convert.ToString(b.getAccOverDraft());
txtNumTrans.Text = Convert.ToString(b.getAccNumTrans());
found = true;
break;
}
}
}
}
Any help appreciated!
If b.getAccNumber()
is returning an int
and searchString
is a string, then it will always return false. For example:
int int_four = 4;
string string_four = "4";
bool eq1 = int_four.Equals(string_four); // false
bool eq2 = int_four.ToString().Equals(string_four); // true
is the "getCustomerNumber" property an actual number (an integer) or not? Which I think its not, because you else wouldn`t use ToUpper() method. There are letter as well (I think).
Anyway, would you mind showing me your Customer class? And paste some customer number example. Mitja
精彩评论