|
-
December 7th, 2008, 12:33 AM
#1
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));
}
-
December 7th, 2008, 04:09 AM
#2
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 = ""; }
-
December 7th, 2008, 10:12 AM
#3
Re: Please Help... Simple Problem I can't solve please!
PHP Code:
DateTime.Now.ToString(@"dddd MMMM dd, yyyy");
-
December 7th, 2008, 11:17 AM
#4
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)
-
December 7th, 2008, 11:51 AM
#5
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
Last edited by eclipsed4utoo; December 7th, 2008 at 11:55 AM.
-
December 7th, 2008, 01:23 PM
#6
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 (....)
-
December 7th, 2008, 01:29 PM
#7
Re: Please Help... Simple Problem I can't solve please!
 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).
Last edited by TheCPUWizard; December 7th, 2008 at 04:33 PM.
Reason: Fix typo pointed out by stel dude...
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
-
December 7th, 2008, 04:13 PM
#8
Re: Please Help... Simple Problem I can't solve please!
 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?
Last edited by STLDude; December 7th, 2008 at 04:16 PM.
-
December 7th, 2008, 04:33 PM
#9
Re: Please Help... Simple Problem I can't solve please!
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
-
December 9th, 2008, 07:02 PM
#10
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.
Three5Eight
Using: MS C# 08 EE, MS SQL 05 EE, C++ .Net 08 EE, Vista Home Premium, XP Home
-
December 9th, 2008, 07:04 PM
#11
Re: Please Help... Simple Problem I can't solve please!
Also, you should be using string.Format on the last line.
Three5Eight
Using: MS C# 08 EE, MS SQL 05 EE, C++ .Net 08 EE, Vista Home Premium, XP Home
-
December 9th, 2008, 07:06 PM
#12
Re: Please Help... Simple Problem I can't solve please!
 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.
-
December 9th, 2008, 07:14 PM
#13
Re: Please Help... Simple Problem I can't solve please!
 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. 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.
Three5Eight
Using: MS C# 08 EE, MS SQL 05 EE, C++ .Net 08 EE, Vista Home Premium, XP Home
-
December 9th, 2008, 07:33 PM
#14
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);
Three5Eight
Using: MS C# 08 EE, MS SQL 05 EE, C++ .Net 08 EE, Vista Home Premium, XP Home
-
December 9th, 2008, 08:02 PM
#15
Re: Please Help... Simple Problem I can't solve please!
 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.
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
|