derek7467
asked on
vb.net IF Statement Loop
I am attempting to loop an If Statement so that if a certain condition is true, it will continuously loop through the if statement until the condition is false. When I attempt to run it with the code below, the loop doesn't happen, no errors, just doesn't happen:
Label4.text is a number that's predetermined, but I manually supply it to test the below code
Label4.text is a number that's predetermined, but I manually supply it to test the below code
Dim test As String = Label4.Text
If test > "0" Then
Do While True
Dim idelay As Integer = 1 * 1000
Me.Timer1.Interval = idelay
Me.Timer1.Enabled = True
delay(2000)
If Me.Label2.Text <= "118985" Then
Label6.Visible = True
Me.Size = New Size(1015, 392)
Button1.Enabled = True
Button1.Text = "Test!"
Button1.ForeColor = Color.Red
Button1.Location = New Point(663, 21)
Me.Timer1.Enabled = False
Me.Label1.Text = "You"
Me.Label2.Text = "Did"
Me.Label3.Text = "It, WOO-HOO PARTY TIME!!!"
Else
Label6.Visible = False
Button1.Enabled = False
End If
Loop
End If
ASKER
Label2.text has a minutes value in it. Not sure 2 being greater than 118985 has anything to do with it.
ASKER
also, if I remove the loop, the if statement fires correctly.
>>Label2.text has a minutes value in it.
It could contain cheese - just make certain you understand comparing two strings does NOT have the same effect as comparing two numbers (or two time values....)
>>also, if I remove the loop, the if statement fires correctly.
I have the feeling your code is working but you are stuck in a permanent loop giving the impression it does not work.
does something like this make a difference
Dim test As String = Label4.Text
dim ContinueLoop as boolean = true
If test > "0" Then
Do While ContinueLoop
Dim idelay As Integer = 1 * 1000
Me.Timer1.Interval = idelay
Me.Timer1.Enabled = True
delay(2000)
If Me.Label2.Text <= "118985" Then
Label6.Visible = True
Me.Size = New Size(1015, 392)
Button1.Enabled = True
Button1.Text = "Test!"
Button1.ForeColor = Color.Red
Button1.Location = New Point(663, 21)
Me.Timer1.Enabled = False
Me.Label1.Text = "You"
Me.Label2.Text = "Did"
Me.Label3.Text = "It, WOO-HOO PARTY TIME!!!"
ContinueLoop = false
Else
Label6.Visible = False
Button1.Enabled = False
End If
Loop
End If
It could contain cheese - just make certain you understand comparing two strings does NOT have the same effect as comparing two numbers (or two time values....)
>>also, if I remove the loop, the if statement fires correctly.
I have the feeling your code is working but you are stuck in a permanent loop giving the impression it does not work.
does something like this make a difference
Dim test As String = Label4.Text
dim ContinueLoop as boolean = true
If test > "0" Then
Do While ContinueLoop
Dim idelay As Integer = 1 * 1000
Me.Timer1.Interval = idelay
Me.Timer1.Enabled = True
delay(2000)
If Me.Label2.Text <= "118985" Then
Label6.Visible = True
Me.Size = New Size(1015, 392)
Button1.Enabled = True
Button1.Text = "Test!"
Button1.ForeColor = Color.Red
Button1.Location = New Point(663, 21)
Me.Timer1.Enabled = False
Me.Label1.Text = "You"
Me.Label2.Text = "Did"
Me.Label3.Text = "It, WOO-HOO PARTY TIME!!!"
ContinueLoop = false
Else
Label6.Visible = False
Button1.Enabled = False
End If
Loop
End If
No, your code is not running, it is just blocking the UI and run forever because you don't have a break in the while loop and you don't cast the label value as an int and also you don't increment it.So, if you adapt your code like this, should work.
So in this code, consider x as your label4 value cast to int.
Dim x As Integer = 25
If test > "0" Then
Do While True
Dim idelay As Integer = 1 * 1000
System.Threading.Thread.Sleep(100)
If x <= CInt("100") Then 'bad practice though
Me.Size = New Size(1015, 392)
Button1.Enabled = True
Button1.Text = "Test!"
Button1.ForeColor = Color.Red
Button1.Location = New Point(663, 21)
Me.Label1.Text = "You"
Me.Label2.Text = "Did"
Me.Label3.Text = "It, WOO-HOO PARTY TIME!!!"
Return
Else
Button1.Enabled = False
End If
x = x + 1
Loop
End If
So in this code, consider x as your label4 value cast to int.
You might have an endless loop that keeps repeating the same thing, so you do not see any change.
First, you never get out of the loop, which makes it run continuously.
Also, you have to be aware that when you are in a loop, the loop executes before anything else. Even if there are events firing in the application, they are not executed until the loop is finished. So your timer events are accumulated and will fire only when you get out of the loop. Since you never get out of the loop, they never fire.
One way to prevent that is to use an Application.DoEvents in the loop. This forces the loop to temporarily stop, processes the events, and then come back to continue looping.
I also wonder why do you need to loop for an operation that basically repeats itself. Except for the delay, nothing significant changes, unless it is done in the Timer event, which does not fire as I explained.
First, you never get out of the loop, which makes it run continuously.
Also, you have to be aware that when you are in a loop, the loop executes before anything else. Even if there are events firing in the application, they are not executed until the loop is finished. So your timer events are accumulated and will fire only when you get out of the loop. Since you never get out of the loop, they never fire.
One way to prevent that is to use an Application.DoEvents in the loop. This forces the loop to temporarily stop, processes the events, and then come back to continue looping.
I also wonder why do you need to loop for an operation that basically repeats itself. Except for the delay, nothing significant changes, unless it is done in the Timer event, which does not fire as I explained.
ASKER
Well, once a certain minute is reached, I perform another activity in the if statement. Basically im counting down to a day in august. Right now its just a timer, which counts down days/minutes/seconds, the reason I want a loop is because once minutes hits a certain time, at that point I execute other activities.
And your timer event is never triggered because you are in a loop.
There is an easy way of checking if your loop is running or not. Simply add a Debug.Writeline("something ") command after you increment x. Run the application inside of Visual Studio, trigger the call to the code, and give a look at the Output windows that shows at the bottom of the screen by default or can be activated through the Debug menu is you do not see it.
If you see a repeating "something" in the Output window, then you know that your loop is running. Try an Application.DoEvents at the end of the loop and see if it solves your problem.
An why initialize the delay with a multiplication? Why not simply give it a straight value?
What does delay(2000) does? Does your code works without it?
There is an easy way of checking if your loop is running or not. Simply add a Debug.Writeline("something
If you see a repeating "something" in the Output window, then you know that your loop is running. Try an Application.DoEvents at the end of the loop and see if it solves your problem.
An why initialize the delay with a multiplication? Why not simply give it a straight value?
What does delay(2000) does? Does your code works without it?
As I mentioned earlier you are stuck in a permanent loop giving you the impression that it is not working.
>>Well, once a certain minute is reached, I perform another activity in the if statement. Basically im counting down to a day in august. Right now its just a timer, which counts down days/minutes/seconds, the reason I want a loop is because once minutes hits a certain time, at that point I execute other activities.
Anyway - it isn't working, you have the wrong logic. If you want this in the timer event you do NOT require the do while loop. (You even said yourself it did work when you didn't use the loop).
>>Well, once a certain minute is reached, I perform another activity in the if statement. Basically im counting down to a day in august. Right now its just a timer, which counts down days/minutes/seconds, the reason I want a loop is because once minutes hits a certain time, at that point I execute other activities.
Anyway - it isn't working, you have the wrong logic. If you want this in the timer event you do NOT require the do while loop. (You even said yourself it did work when you didn't use the loop).
ASKER
Is there a better way to do this?
Basically when the timer hits a certain time in minutes (initiated onload), I want it to fire off a method I created.
Basically when the timer hits a certain time in minutes (initiated onload), I want it to fire off a method I created.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
When I moved my code to the tick event, everything fired off correctly.
James, you were right, I didn't entirely know everything a "Timer" actually does... THANK YOU!
James, you were right, I didn't entirely know everything a "Timer" actually does... THANK YOU!
A couple of points.
What you asked in your question is not what you wanted to know - asking what you want will often get you a better response.
Learning by doing is good, BUT use the help or a book about programming. If you don't know something exists then you certainly will not use it.
What you asked in your question is not what you wanted to know - asking what you want will often get you a better response.
Learning by doing is good, BUT use the help or a book about programming. If you don't know something exists then you certainly will not use it.
ASKER
Andy I disagree with you. Yes my question was kinda confusing but after working with James I got exactly what I needed. I pay a price to use this site and don't just ask for answers so if you have a problem helping out when things get confusing I'd suggest you check yourself and decide whether or not you belong on this site.
Confusing yes.
Have you reread your question:
I am attempting to loop an If Statement so that if a certain condition is true, it will continuously loop through the if statement until the condition is false. When I attempt to run it with the code below, the loop doesn't happen, no errors, just doesn't happen:
Nothing about using a timer that I see there. Also note that two experts pointed out the same error in that code you had (the loop actually was working but stopping the UI from updating), James later also pointed out the same error.
>>if you have a problem helping out when things get confusing
No problems here, my previous comment was trying to help you in your confusion. ( I must do something. This is something. Therefore I must do this. Can you spot what is wrong with that approach? )
Have you reread your question:
I am attempting to loop an If Statement so that if a certain condition is true, it will continuously loop through the if statement until the condition is false. When I attempt to run it with the code below, the loop doesn't happen, no errors, just doesn't happen:
Nothing about using a timer that I see there. Also note that two experts pointed out the same error in that code you had (the loop actually was working but stopping the UI from updating), James later also pointed out the same error.
>>if you have a problem helping out when things get confusing
No problems here, my previous comment was trying to help you in your confusion. ( I must do something. This is something. Therefore I must do this. Can you spot what is wrong with that approach? )
If Me.Label2.Text <= "118985" Then
which means that "2" is GREATER then "118985". Is that what you want?
I think you need to convert to integer values
if CInt(Me.Label2.Text) <= 118985 then