Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
Marcham89
Okay thanks. Do you always need an "else" statement? (because for in this instance I know it cant be anything else)
You know, but the compiler doesnt know. That's why it's complaining.
Going back to what buser hinted at, why are you writing 12 IFs when you can just write:
Month = DateTime.Now.ToString("MMMM");
If you really must use 12 IFs, what's wrong with:
Code:
int month = DateTime.Now.Month;
if(month == 1)
..
elseif(month == 11)
..
else
..
Why convert everything to string all the time?
Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
TheCPUWizard
While a switch statement may "look cleaner" it is exactly the same performance for "if elseif else" constructs, UNLESS the switch parameter is an integral value, and the states have "tight grouping".
Interesting, I read in a book some place. I think it was "C# CLR" that elseif statements add extra overhead because they cause the same expression to be evaluated over and over again. Where a switch statement only requires it to be evaluated once.
But that book was designed to tweak every single millisecond out of your program. So your probably right that the difference might not be noticable in this piece of code. :)
Myself, I would still use the switch statement because it just seems more correct to me. If I was going to do something in real life, like solve a math problem and compare it to another value, I would solve the problem write it down, and then compare it. I would not solve the same problem over and over again before I compared it each time. That would be pointless in my mind.
But anyway there is no need to convert ToString() each time, and int will do just the same job and wont have the extra conversion. Like I shown in my example.
Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
Three5Eight
Interesting, I read in a book some place. I think it was "C# CLR" that elseif statements add extra overhead because they cause the same expression to be evaluated over and over again. Where a switch statement only requires it to be evaluated once..
The fidderefnce could be considerable it there was something to EVALUATE (we are not talking about the comparison, but rather the evaluation of the term)
Code:
int ReallyLong() { Sleep(100); return 3); }
if (ReallyLong() == 1) {}
else if if (ReallyLong() == 2) {}
else if if (ReallyLong() == 3) {}
Now that (albiet really stupid code) would benefit from a"switch"