Whats up with this?

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

Hi all,

whats wrong with the following code:

DateTime calcDate = new DateTime();

DateTime dDate = DateTime.Now;

if (calcDate.DayOfWeek(dDate) != "Saturday") || (calcDate.DayOfWeek(dDate)
!= "Sunday")

{

}



Thanks,

Jim
 
umm..not sure what you are trying to do...but I'll suggest anyway.

DateTime dateTime = DateTime.Now;

if (dateTime.DayOfWeek == DayOfWeek.Tuesday)
{
Console.WriteLine("Yes it is tuesday");
}
 
Jim,
whats wrong with the following code:

DateTime calcDate = new DateTime();

DateTime dDate = DateTime.Now;

if (calcDate.DayOfWeek(dDate) != "Saturday") || (calcDate.DayOfWeek(dDate)
!= "Sunday")

{

}


DayOfWeek is a property, you're using it like a method. It returns a
value of the enum type DayOfWeek, not a string. Try it like this

if (dDate.DayOfWeek != DayOfWeek.Saturday ||
dDate.DayOfWeek != DayOfWeek.Sunday)

But then, that code is pretty much useless, since it will always
evaluate to true.

What did you actually want to accomplish?



Mattias
 
Hi Matt,

Trying to detect in if statement that its not Sat or Sun do the
following code.
So I should use the logical and " && " instead, correct?

Thanks,

JJ
 
Ah it was the brackets around the expressions that was causing the Invalid
expression.

But at the same time Mattias you were saying that my logic with the if was
going to fail anyway because of using Or correct?

I should be using the and operator && instead in order for the logic to
work, correct?

Thanks,
JJ
 
Gotcha thanks John !

JJ


Jon Skeet said:
Yes. Look at your code carefully, and think about what it means: you're
specifying that you want something done either if it's not Sunday, or
it's not Saturday. It'll *always* be either not Sunday or not Saturday,
because it can't be both Saturday and Sunday at the same time.


Either that or rephrase it as (in pseudo-code)

if (! (day==Saturday || day==Sunday) )
{
}
 
Back
Top