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
VulpesPosted Oct 5, 2011, 7:19 AM
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.
VulpesPosted Oct 6, 2011, 4:27 AM
When something in a foreach is not happening but should happen, then this is often the reason why.
RichPosted Oct 5, 2011, 6:52 PM
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
Suthish NairPosted Oct 5, 2011, 1:04 PM
Sam HobbsPosted Oct 5, 2011, 12:07 PM
if (c == 'a' || c == 'e')
Console.Write('z');
else
Console.Write(c);
RichPosted Oct 5, 2011, 7:50 AM
Prabhu RajaPosted Oct 5, 2011, 7:31 AM
You may first Check Username and Password Value after entering into foreach loop by,
Prabhu RajaPosted Oct 5, 2011, 7:10 AM
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.
RichPosted Oct 5, 2011, 7:06 AM
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.
VulpesPosted Oct 5, 2011, 6:48 AM
RichPosted Oct 5, 2011, 6:41 AM
VulpesPosted Oct 5, 2011, 6:38 AM
The output should be : zbcdzfgh
Rakesh JhaPosted Oct 5, 2011, 6:34 AM
you can also use switch statement but i suppose you should include your code, so that we can easily identify error.
VulpesPosted Oct 5, 2011, 6:30 AM
If you can post your code, then we can take a look at it.