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.