Reputation: 5
string number = txtNumber.Text;
foreach (Account ac in tabAccounts)
{
if (txtNumber.Text == ac.Number)
{
this.Height = 328;
lblWelcome.Text = "Welcome: " + ac.Client;
break;
}
else
{
MessageBox.Show("Account Number not found");
}
}
Hello everyone, I'm fairly new here and with C# . So I have a class Account with client info which is stored in a text file.I want to loop through the array tabAccounts[200] and look if the entered user number corresponds to one that is in the text file. It works fine but when i enter let's say 222 it starts looping from the begging until it finds the number and if it doesn't it just keep looping and the message "Account Number not found" keeps coming out. When i remove the else statement it works fine but I want it that when a user enters a wrong number a message box will show...Hope you guys get it :( Tried googling but didnt find anything..
Upvotes: 1
Views: 71
Reputation: 69280
This can be done much more effective with LINQ:
var account = tabAccounts.SingleOrDefault(a => a.Number == txtNumber.Text);
if(account != null)
{
this.Height = 328;
lblWelcome.Text = "Welcome: " + account.Client;
}
else
{
MessageBox.Show("Account Number not found");
}
For your original code: The problem is that you are displaying the message inside the loop, every time the loop didn't find anything. This is a rewrite close to your original syntax, just to show you how to do it:
Account foundAccount = null;
foreach (Account ac in tabAccounts)
{
if (txtNumber.Text == ac.Number)
{
foundAccount = ac;
break;
}
}
if(foundAccount != null)
{
this.Height = 328;
lblWelcome.Text = "Welcome: " + foundAccount.Client;
}
else
{
MessageBox.Show("Account Number not found");
}
Upvotes: 7
Reputation: 56566
The problem is that you don't wait until you've finished searching to display "Account Number not found"
, you show it every time you find an Account
that doesn't match. I'd rewrite it like this, using the FirstOrDefault
extension method.
string number = txtNumber.Text;
Account ac = tabAccounts.FirstOrDefault(x => x.Number == txtNumber.Text);
if (ac != null)
{
this.Height = 328;
lblWelcome.Text = "Welcome: " + ac.Client;
}
else
{
MessageBox.Show("Account Number not found");
}
Upvotes: 3
Reputation: 11903
The minimum change required to achive your goal:
string number = txtNumber.Text;
bool found = false;
foreach (Account ac in tabAccounts)
{
if (txtNumber.Text == ac.Number)
{
this.Height = 328;
lblWelcome.Text = "Welcome: " + ac.Client;
found = true;
break;
}
}
if (!found)
MessageBox.Show("Account Number not found");
Upvotes: 3
Reputation: 2431
The best way to do this would be to set a flag when you find the element and then display outside of the loop
bool found = false;
string client;
string number = txtNumber.Text;
foreach (Account ac in tabAccounts)
{
if (txtNumber.Text == ac.Number)
{
found = true;
client = ac.Client;
break;
}
}
if (found)
{
this.Height = 328;
lblWelcome.Text = "Welcome: " + client;
}
else
{
MessageBox.Show("Account Number not found");
}
Upvotes: 1