|
-
December 9th, 2008, 08:24 PM
#16
Re: Please Help... Simple Problem I can't solve please!
 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?
Last edited by cjard; December 9th, 2008 at 08:27 PM.
-
December 9th, 2008, 10:14 PM
#17
Re: Please Help... Simple Problem I can't solve please!
 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.
Three5Eight
Using: MS C# 08 EE, MS SQL 05 EE, C++ .Net 08 EE, Vista Home Premium, XP Home
-
December 9th, 2008, 11:22 PM
#18
Re: Please Help... Simple Problem I can't solve please!
 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"
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|