The else is catching:
daily=true and DayOfWeek=1
when you end up on that you never move Date_Run.Day + 1 so you end up stuck in the loop
Julian already answered, I'm just testing and learning.
So your options are:
a) RB_Kind_All, always add 1 day
b) RB_Kind_Sunday, add 7 days for DayOfWeek = 1
c) RB_Kind_Daily, add 1 day except Sunday when you add 2
a) if it is Sunday, it could be 7 or 2 days to add (_Sunday or _Daily)
b) any other day you add 1 day
I think this code will work:
// Day additions (use the RadioButton selection) If Date_Run.DayOfWeek = 1 Then If RB_Kind_Sunday.Value Then Date_Run.Day = Date_Run.Day + 7 Elseif RB_Kind_Daily.Value Then Date_Run.Day = Date_Run.Day + 2 End If Else Date_Run.Day = Date_Run.Day + 1 End If
Edit: I think my code is different, you are not adding 2 to RB_Kind_Daily on Sunday. I think you code will add 1 if RB_Kind_Daily is selected and DayOfWeek <> 1, but it also will add 1 if RB_Kind_Daily is selected and DayOfWeek = 1 via Else, making it the same as RB_Kind_All.
Emile, I suggest you restructure the code a bit to help you think more clearly about it.
// Day additions (use the RadioButton selection) If RB_Kind_Daily.Value Then If Date_Run.DayOfWeek <> 1 Then // Daily Strips only [No Sunday] Date_Run.Day = Date_Run.Day + 1 Else // It's Sunday, what should I do??? End If ElseIf RB_Kind_Sunday.Value Then // Sunday Strips only Date_Run.Day = Date_Run.Day + 7 ElseIf RB_Kind_All.Value Then // Daily and Sunday Strips [7 days a week] Date_Run.Day = Date_Run.Day + 1 Else // None of the RadioButtons are selected??? End If
As Julian points out, if your loop depends on Date_Run being incremented, then you really only have 2 conditions if you want to avoid an infinite loop:
Give some thought to how your loop should function, there may be a better way.
Thank you all for your answers.
@Tim H Everything else, add 1
No. There are two cases here: the condition includes Sundays OR excludes Sundays (rare but existant).
Julian: I am usure if I really understand what you wrote. I added the Else case (unneeded) just to be sure one day inc is done / the loop exits somewhere… Having a value of 60K or more after cmd-. is impossible. In my testings, I often use cases where I know the results before trying in unknow results.
THe real code does not had the Else statement.
Also, I may not be clear, bu the Else in my code was a despair attempt to make the code running (or better: to know why the Day is not incremented…)
This is a simple case with three possible solutions, but I have a blockage with it. I will try with Mon-Sat and Sunday only (two tests) to know what happens.
I make a new test and I was creative (!)
The RadioButton Value set to True is RB_Kind_Daily (the Else in the code below)
If RB_Kind_Sunday.Value Then // Sunday Strips only Date_Run.Day = Date_Run.Day + 7 Else // Daily Strips only Date_Run.Day = Date_Run.Day + 1 End If
If the current day is not a Sunday I add 1 day to
Date_Run.Day, else I add 7 days to
This failed too !
FWIW, here’s the used data:
Matrix Story Start End Length 0 2016-01-01 2016-01-12 12 1 2017-01-01 2017-01-20 375 2 2018-01-01 2018-01-31 377
Obviously, there cannot be 12 days (in Row 0) between January 1st and January 12th, same apply for the two other Rows ! :(
Doh ! I forgot to follow Tim’s advice. I will do that in some minutes after the current download is over.
Sometimes the loop does not exits !
Now the loop adds 1 day two times, then falls in a Sunday and continue that way not following the RadioButton.Value…
I give up for… sometimes until a good idea comes to mind !
(or a Muse tell something to my ear…)
This may be the whole method design who is wrong.
A “simple” day counter between two dates !
Dave, thank you, I didn't know you can use an If statement like that. Definitely I learn a lot just by reading the forum.
Emile, maybe you can share a sample project with a little more information and not just the if..then elseif..., I think with that someone can tell you what is happening.
Don't mind the code below. I wrote this before I completely read your last post: "Sometimes the loop does not exits !". Just post a sample with the loop and I'm sure someone will help you.
Your original code check:
1.- If RB_Kind_Daily AND DayOfWeek <> 1, Date_Run.Day + 1
I think this will only happen when the radio button is in Daily and DayOfWeek <> 1 (2-7)
2.- if RB_Kind_Sunday, Date_Run.Day + 7
I think this will happen any day just for the radio button set to Sunday, even if DayOfWeek is 2-7
3.- if RB_Kind_All, Date_Run.Day+1
I think this will happen for every DayOfWeek
You had to add the Else to catch RB_Kind_Daily AND DayOfWeek = 1. Without it you will end in a loop that will no add 1 to Date_Run.Day.
Because you added the Else statement, now your If...Then ElseIf statement works just like:
If RB_Kind_Sunday.Value Then // This executes even if Date_Run.DayOfWeek <> 1 Date_Run.Day = Date_Run.Day + 7 Else Date_Run.Day = Date_Run.Day + 1 End If
Maybe someone with more experience can check this.
@Dave S Date_Run.Day=Date_Run+IF(RB_Kind_Sunday.value,7,1)
I do not understand that. I will read it once more later, maybe…
I found two errors in my
a. An Else is mandatory: when the day is a Sunday, the code do not add 1 day to go to the following Monday; that is why I get an infinite loop !
b. The ellapsed number of days addition have to be in the If block, else I get a +1 in each do Loop iteration…
The program seems to work now (at least for Dailies: I checked with a large file who already have the calculations in a column…).
BTW: the used If block had only two tests (Daily without Sunday and Sunday: I have to add the third one…)
More testings, but I think I get it.
FWIW: I totally rewrite from scratch the method (who compute a number of days between two dates with or without Sundays…)
Ok.... let me see if I can explain a simple addition
Note : I did leave ".day" out by accident...
Date_Run.Day = Date_Run.day + IF(RB_Kind_Sunday.value,7,1)
Failing to see where the issue here is..... a simple read of the Language Ref would have explained it just as well
I saw this come out in release notes or the XDEV magazine. The explanation for the If Operator (If Operator )does show
result = If(expression, resultWhenTrue, resultWhenFalse)
None of them use "+" so how do you do that?
How or where is this documented?
I am glad I saw this because I didn't think I could get this on one line.
result = If(expression, resultWhenTrue, resultWhenFalse) was added ?
I searched from 2015r2 (it does not exists in 2015r1) in the
New Items parts of the Release Notes and found nothing.
Also, this information does not appears in the LR entry.
So, in what Xojo version
result = If(expression, resultWhenTrue, resultWhenFalse) appears ?
@Emile S So, in what Xojo version
result = If(expression, resultWhenTrue, resultWhenFalse)appears ?
Xojo 2014 Release 1