Please Help... Simple Problem I can't solve please!
I don't understand why there is an error saying that the local variable (Month) is not being assigned and I don't understand why.
Code:
String Month;
if (DateTime.Now.Month.ToString() == "1")
{
Month = "January";
}
else if (DateTime.Now.Month.ToString() == "2")
{
Month = "February";
}
else if (DateTime.Now.Month.ToString() == "3")
{
Month = "March";
}
else if (DateTime.Now.Month.ToString() == "4")
{
Month = "April";
}
else if (DateTime.Now.Month.ToString() == "5")
{
Month = "May";
}
else if (DateTime.Now.Month.ToString() == "6")
{
Month = "June";
}
else if (DateTime.Now.Month.ToString() == "7")
{
Month = "July";
}
else if (DateTime.Now.Month.ToString() == "8")
{
Month = "August";
}
else if (DateTime.Now.Month.ToString() == "9")
{
Month = "September";
}
else if (DateTime.Now.Month.ToString() == "10")
{
Month = "October";
}
else if (DateTime.Now.Month.ToString() == "11")
{
Month = "November";
}
else if (DateTime.Now.Month.ToString() == "12")
{
Month = "December";
}
MessageBox.Show((DateTime.Now.DayOfWeek.ToString()) + " " + Month + " " + (DateTime.Now.Day) + ", " + (DateTime.Now.Year));
}
Re: Please Help... Simple Problem I can't solve please!
Because the case can be where none of the if/else if statement could be executed, so compiler detects that. You have couple choice here:
PHP Code:
String Month = "";
or
PHP Code:
// ... snipped
else if (DateTime.Now.Month.ToString() == "12")
{
Month = "December";
}
else
{
Month = "";
}
Re: Please Help... Simple Problem I can't solve please!
PHP Code:
DateTime.Now.ToString(@"dddd MMMM dd, yyyy");
Re: Please Help... Simple Problem I can't solve please!
Okay thanks. Do you always need an "else" statement? (because for in this instance I know it cant be anything else)
Re: Please Help... Simple Problem I can't solve please!
maybe an easier way of getting it?
Code:
DateTime dt = DateTime.Now;
string test = string.Format("{0:y}", dt); // Formats as "December, 2008"
string[] array = test.Split(new char[] {',' });
string month = array[0];
Console.WriteLine(month);
Console.ReadLine();
Also, there are a number of different formats that .Net will automatically create. Check out the MSDN article..
http://msdn.microsoft.com/en-us/libr...d4(VS.80).aspx
Re: Please Help... Simple Problem I can't solve please!
You must assign a value to Month before going into the if statements. Because the compiler doesn't know if any of the if statements will be true and the assignment executed. Even if it is obvious that there can be no month == "33" or something.
so it goes like:
String Month;
Month = "??error??";
if (....)
Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
dotnet9
You must assign a value to Month before going into the if statements.
NOT true. Go back and re-read the previous responses. All you MUST to is ensure that there is NO possible path through the code where the value is READ before it is Written.
Doing an assignment before the if's would accomplish this, but since the value would be properly assigned for all legal inputs by the if statements, putting a trailing else is the proper thing to do.
However, this trailing if else should almost certainly NOT be an assignment. It should be the throwing of an execption (InvalidArgumentEXception).
Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
TheCPUWizard
However, this trailing if should almost certainly NOT be an assignment. It should be the throwing of an execption (InvalidArgumentEXception).
Good point.
Didn't you mean this trailing if should almost to be this trailing else should almost?
Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
STLDude
Good point.
Didn't you mean this trailing if should almost to be this trailing else should almost?
:blush::blush::blush::blush:
Re: Please Help... Simple Problem I can't solve please!
But why all the if, elseif statements, thats very bad performance for your application. You should be using a switch. It is much clearer, an if elseif block was not designed for what you are doing.
When you use this block you should be evaluating something different on each statement. You are evaluating the same thing over and over again, you should be using a switch. This way you evaluate only one time. Its better for performance as well.
How many time are you using ToString here, that is going to slow your program waaaaay down.
Re: Please Help... Simple Problem I can't solve please!
Also, you should be using string.Format on the last line.
Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
Three5Eight
But why all the if, elseif statements, thats very bad performance for your application. You should be using a switch. It is much clearer, an if elseif block was not designed for what you are doing.
When you use this block you should be evaluating something different on each statement. You are evaluating the same thing over and over again, you should be using a switch. This way you evaluate only one time. Its better for performance as well.
How many time are you using ToString here, that is going to slow your program waaaaay down.
It is inefficient, but I seriously doubt that you will see a performance impact unless it is being run constantly.
Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
BigEd781
It is inefficient, but I seriously doubt that you will see a performance impact unless it is being run constantly.
But an if statement wasnt intended to be used like that. :confused: It was designed when you need to test something different. Since you are testing the same thing, why do it over and over again.
ToString() can be a heavy process if you are using it that many times. there are 11 unneeded conversions in that one code block alone.
I agree maybe this wont cause a noticable performance hit on this code, but if he writes an enterprise level application and then entire thing is coded like this. You can't tell me that there wont be a huge performance hit.
Re: Please Help... Simple Problem I can't solve please!
I don't even know what the code is supposed to do because it's not explained. But it seems that there is a build in method to turn a date month into a string without even using a switch. Please post an explanation of what the code is intended to do.
This is better:
Code:
String Month = string.Empty;
switch(DateTime.Now.Month)
{
case 1:
Month = "Jan"; break;
case 2:
Month = "Feb"; break;
case 3:
Month = "Mar"; break;
case 4:
Month = "Apr"; break;
case 5:
Month = "May"; break;
case 6:
Month = "June"; break;
case 7:
Month = "July"; break;
case 8:
Month = "Aug"; break;
case 9:
Month = "Sept"; break;
case 10:
Month = "Oct"; break;
case 11:
Month = "Nov"; break;
case 12:
Month = "Dec"; break;
default: break;
}
string part1 = string.Format(string.Concat(DateTime.Now.DayOfWeek.ToString(), " {0}"), Month);
string part2 = string.Concat(part1, DateTime.Now.Day, " ", DateTime.Now.Year);
MessageBox.Show(part2);
Re: Please Help... Simple Problem I can't solve please!
Quote:
Originally Posted by
Three5Eight
But why all the if, elseif statements, thats very bad performance for your application. You should be using a switch. It is much clearer, an if elseif block was not designed for what you are doing.
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".
Therefore something like "1,2,3,4,5" will be faster (but not measurably different unless you have a few thousand conditions (the nested elses add about 0.4uS each on a 2.4Ghz processor assuming the code is already in the L1 cache. You would have to have approximately 2000 of them in order to see a 1/1000 second difference.
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"