if else statement - DayOfWeek

  • Thread starter Thread starter 7777
  • Start date Start date
7

7777

Hello, sorry if this is novice as I'm a newbie to c#. I keep getting
compiling erros with the following, could someone point out what is wrong as
I'm trying to find the day of week it's on and then subtract 7 days if any
of the conditions are true?

If (Temp_datetime.DayOfWeek == DayOfWeek.Tuesday || Temp_datetime.DayOfWeek
== DayOfWeek.Wednesday || Temp_datetime.DayOfWeek == DayOfWeek.Thursday ||
Temp_datetime.DayOfWeek == DayOfWeek.Friday || Temp_datetime.DayOfWeek ==
DayOfWeek.Saturday)
Temp_Recurrence_range.Start = Temp_datetime - new
TimeSpan(7, 0, 0, 0);
else
Temp_Recurrence_range.Start = Temp_datetime;


Thanks in advance.
 
7777 said:
Hello, sorry if this is novice as I'm a newbie to c#. I keep getting
compiling erros with the following, could someone point out what is wrong as
I'm trying to find the day of week it's on and then subtract 7 days if any
of the conditions are true?

If (Temp_datetime.DayOfWeek == DayOfWeek.Tuesday || Temp_datetime.DayOfWeek
== DayOfWeek.Wednesday || Temp_datetime.DayOfWeek == DayOfWeek.Thursday ||
Temp_datetime.DayOfWeek == DayOfWeek.Friday || Temp_datetime.DayOfWeek ==
DayOfWeek.Saturday)
Temp_Recurrence_range.Start = Temp_datetime - new
TimeSpan(7, 0, 0, 0);
else
Temp_Recurrence_range.Start = Temp_datetime;


Thanks in advance.

C# is case sensetive. Change If to if.
 
Hello, sorry if this is novice as I'm a newbie to c#.  I keep getting
compiling erros with the following, could someone point out what is wrongas
I'm trying to find the day of week it's on and then subtract 7 days if any
of the conditions are true?

If (Temp_datetime.DayOfWeek == DayOfWeek.Tuesday || Temp_datetime.DayOfWeek
== DayOfWeek.Wednesday || Temp_datetime.DayOfWeek == DayOfWeek.Thursday ||
Temp_datetime.DayOfWeek == DayOfWeek.Friday || Temp_datetime.DayOfWeek ==
DayOfWeek.Saturday)
                Temp_Recurrence_range.Start = Temp_datetime - new
TimeSpan(7, 0, 0, 0);
            else
                Temp_Recurrence_range.Start = Temp_datetime;

Thanks in advance.

The first thing that I see is that the 'I' is capitalized in your "if"
statement. It should be all lowercase.

That fix it? if that was just a cut and paste error, what's the
compile error?
 
Oh that's right I do recall hearing that. It now works. Thanks a bunch
Goran for your speedy helpful reply. Cheers :-)
 
It had the little squiggly at the end of the If line and mentioning
'expected' in the compiler error list and also mentioned an invalid 'else'
error which now makes sense since it's case sensitive. Thanks for the quick
replies gentlemen :-)


Hello, sorry if this is novice as I'm a newbie to c#. I keep getting
compiling erros with the following, could someone point out what is wrong
as
I'm trying to find the day of week it's on and then subtract 7 days if any
of the conditions are true?

If (Temp_datetime.DayOfWeek == DayOfWeek.Tuesday ||
Temp_datetime.DayOfWeek
== DayOfWeek.Wednesday || Temp_datetime.DayOfWeek == DayOfWeek.Thursday ||
Temp_datetime.DayOfWeek == DayOfWeek.Friday || Temp_datetime.DayOfWeek ==
DayOfWeek.Saturday)
Temp_Recurrence_range.Start = Temp_datetime - new
TimeSpan(7, 0, 0, 0);
else
Temp_Recurrence_range.Start = Temp_datetime;

Thanks in advance.

The first thing that I see is that the 'I' is capitalized in your "if"
statement. It should be all lowercase.

That fix it? if that was just a cut and paste error, what's the
compile error?
 
Hi

Forgive me my ignorance, but why not simple

if (!(Temp_datetime.DayOfWeek == DayOfWeek.Monday) &&
!(Temp_datetime.DayOfWeek == DayOfWeek.Sunday))

Cor
 
Hi Cor, thanks for your alternative suggestion in trying to help me simplify
things. Your code would appear to work but since I'm going through each
work day I guess your code would appear to expand larger in going the
opposite direction when it would need to check for Friday. I'm checking to
see if Temp_datetime which holds a RotationPeriod start date falls on a
particular day of week which involves a calendar control for recurring
appointments that perform every other week (weekly recurrence interval = 2).
This calendar control is behaving a little off in what I'm aiming for in
that if the RotationPeriod start date (Temp_datetime) falls on a day after a
particular day of the week then it's still counting that intial week as a
1st recurrence which then it actually then skips the immediate day of week
of the following week in where as my work around is to re-assign the
recurrence start date a week earlier when appropriate with my if else
statements here from the actual appointment start date. Hope I didn't
confuse you but at least it looks like my work around is working for now
which gives me a bit of relief. Thanks Cor :-)
 
7777 said:
Oh that's right I do recall hearing that. It now works. Thanks a bunch
Goran for your speedy helpful reply. Cheers :-)

Nice. :)

An alternative way of comparing one value to several other is using a
switch:

switch (Temp_datetime.DayOfWeek) {
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
case DayOfWeek.Saturday:
Temp_Recurrence_range.Start = Temp_datetime.AddDays(-7);
break;
default:
Temp_Recurrence_range.Start = Temp_datetime;
break;
}
 
Back
Top