Greetings,
I am new to c# programming. Is it possible to include a conditional if else statement nested within a foreach iteration. I've been working on this the last couple of days to no avail. The if part of the statement works, but the else statement seems to get skipped. Just need some confirmation on the matter. Thanks in advance to all.
Rich
Loading

VulpesRank #55 · 29.2K reputationPosted Oct 5, 2011, 3:19 AM EDT
I'd try inserting this line just before the foreach:
MessageBox.Show(query.Count().ToString());
I always paste code from VS into notepad first and from there into the reply box. Otherwise, it tends to be all over the place as you say.
VulpesRank #55 · 29.2K reputationPosted Oct 5, 2011, 2:30 AM EDT
If you can post your code, then we can take a look at it.
Rakesh Jha85 reputationPosted Oct 5, 2011, 2:34 AM EDT
you can also use switch statement but i suppose you should include your code, so that we can easily identify error.
VulpesRank #55 · 29.2K reputationPosted Oct 5, 2011, 2:38 AM EDT
The output should be : zbcdzfgh
Rich83 reputationPosted Oct 5, 2011, 2:41 AM EDT
foreach (var userItem in query) { if ((userItem.User.ToString() == userName) && (userItem.Password.ToString() == password)) { User SiteDirector = new User(); SiteDirector.DirectorUserName = userItem.User.ToString(); Users.NavDirector NavDirector = new Users.NavDirector(); NavDirector.Show(); } else { string message = "Wrong user name or password."; string caption = "Invalid User Name or Password."; MessageBoxButton Button = MessageBoxButton.OK; MessageBoxImage Icon = MessageBoxImage.Warning; MessageBox.Show(message, caption, Button, Icon); } } I am simply trying to get message box to show when either the password or username is incorrect. I have confirmed that the first part works, as it executes correctly when the appropriate username and password is supplied, but the else statement doesn't seem to be recognized. The program builds with out errors, so I am a bit clueless here.VulpesRank #55 · 29.2K reputationPosted Oct 5, 2011, 2:48 AM EDT
Rich83 reputationPosted Oct 5, 2011, 3:06 AM EDT
of a button click method in a wpf. When i enter the correct information the program proceeds as expected. When I then enter incorrect information
I get no response from the program; I click the button and nothing happens.
On a side note. Is there a certain way to paste the code into this post, or is just manually typing it the best way, as my code is all over the place
in the previous post, lol.
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 5, 2011, 3:10 AM EDT
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Print.
foreach (int value in array)
{
for (int i = 0; i < value; i++)
this.TextBox1.Text += value.ToString();
this.TextBox1.Text += System.Environment.NewLine;
}
// Reverse.
Array.Reverse(array);
// Print.
foreach (int value in array)
{
for (int i = 0; i < value && value != 9; i++)
{
this.TextBox1.Text += value.ToString();
}
if (value != 9 )
this.TextBox1.Text += System.Environment.NewLine;
}
The output will be:
1
22
333
4444
55555
666666
7777777
88888888
999999999
88888888
7777777
666666
55555
4444
333
22
1
The If and for loop works perfectly for me.
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 5, 2011, 3:31 AM EDT
You may first Check Username and Password Value after entering into foreach loop by,
Rich83 reputationPosted Oct 5, 2011, 3:50 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Oct 5, 2011, 8:07 AM EDT
if (c == 'a' || c == 'e')
Console.Write('z');
else
Console.Write(c);
Suthish NairRank #92 · 22.8K reputationPosted Oct 5, 2011, 9:04 AM EDT
Rich83 reputationPosted Oct 5, 2011, 2:52 PM EDT
That solved it! The foreach block never ran when the requested info was invalid as you stated. I moved the message box
from within the else block to in between the linq query and foreach block. here is the fix:
if (query.Count().ToString() == "0")
{
string message = "Wrong user name or password.";
string caption = "Invalid User Name or Password.";
MessageBoxButton Button = MessageBoxButton.OK;
MessageBoxImage Icon = MessageBoxImage.Warning;
MessageBox.Show(message, caption, Button, Icon);
}
I assume since the query returned 0, the foreach block did not have a reason to execute. Thanks for the help and insight from everyone.
Rich
VulpesRank #55 · 29.2K reputationPosted Oct 6, 2011, 12:27 AM EDT
When something in a foreach is not happening but should happen, then this is often the reason why.