Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

switch statement question 6

Status
Not open for further replies.

hedgracer

Programmer
Mar 21, 2001
186
US
I have the following code:

int iMth = DateTime.Now.Month;
int iYr = DateTime.Now.Year;
string sYear;
string sMonth;

switch (iMth)
{
case 1:
if (iMth == 1)
{
iYr = iYr - 1;
sMonth = "12";
sYear = iYr.ToString();
}
break;
case 2:
if (iMth > 1 || iMth < 10)
{
iMth = iMth - 1;
sMonth = "0" + iMth.ToString();
sYear = iYr.ToString();
}
break;
case 3:
if (iMth >= 10)
{
sMonth = iMth.ToString();
sYear = iYr.ToString();
}
break;
}

iMth is equal to 3 and iYr is equal to 2008. The trouble is that iMth is dropping through the switch statement to case 3 even though iMth is 3. I am relatively new to C# so this is a little troubling to me(C++ was several years ago). Can anyone give me some suggestions? Thanks for all help in advance.

Dave
 
If iMth is equal to 3, thats when it will go to case 3, and if (iMth >= 10) == 3 >= 10, which is false. Why not just use a series of if-else statements? There are a couple of threads debating about switches, I'll find them and post them
 
The constant after the word case is the value against which iMth is tested to determine which option to choose.

In your code for case 1 for example, the line if (iMth == 1) is redundant because iMth MUST equal 1 to get there in the first place.

Hope this helps.

[vampire][bat]
 
it looks like your trying to format the date for last month. which is very difficult to read. this might be simpler.
Code:
public DateMonkier
{
public static string GetFirstDayOfLastMonthAsString(DateTime date)
{
   DateTime thisDayLastMonth = date.AddMonths(-1);
   return new DateTime(thisDayLastMonth.Year, thisDayLastMonth.Month, 1).ToString("MM/dd/yy");
}
}
Code:
string dateToDisplay = DateMonkier.GetFirstDayOfLastMonthAsString(DateTime.Today);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
switch is equivalent to if else where ONLY == are used. To get switch to work, you need to evaluate first THEN use the evaluation to drive your code.

Hence...........

int iMth = DateTime.Now.Month;
int iYr = DateTime.Now.Year;
string sYear;
string sMonth;

int sec = 3;

if(iMth<10)
{
sec = 2;
}

if(iMth<2)
{
sec = 1;
}

switch(sec)
{
case 1:
//Dostuff
break;
case 2:
//Dostuff
break;
case 3:
//Dostuff
break;
}

C
 
I don't get the need of debate on switch/if-else! Op's line is simply ill-conceived (plus some redundant as pointed out) and can be corrected like this which preserves op's idea of using switch.
[tt]
switch ([red]true[/red])
{
case [red](iMth == 1)[/red]:
iYr = iYr - 1;
sMonth = "12";
sYear = iYr.ToString();
break;
case [red](iMth > 1 || iMth < 10)[/red]:
iMth = iMth - 1;
sMonth = "0" + iMth.ToString();
sYear = iYr.ToString();
break;
case [red](iMth >= 10)[/red]:
sMonth = iMth.ToString();
sYear = iYr.ToString();
break;
}
[/tt]
 
interseting use of switch.
while the discussion is good, the original code above appears to be a formatting issue. in reading the logic above it appears:
1. subtract 1 month from the given date.
2. format to 2 digit month, 4 digit year.
Code:
DateTime.Now.AddMonth(-1).ToString("MMYYYY");

this would be much simpiler given the function above. granted i made some assumptions about the date, but that is much eaiser (both to read and write) then messing around with switch/if statements.

I believe the problem is that the developer is trying to minipulate dates as numbers instead of dates. if you treat a date like a date to preform the calculations and formatting on the date, then the logic and code become much simpiler.


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Interesting is one way to put it.

First, dealing with dates as dates appears to be the way to go here (although the OP has not been back to confirm, I cannot conceive how it would be otherwise), and I would argue that perpetuating this (mis)use of the switch statement is actually doing the OP a disservice, and not offering helpful advice.

Second, if the OP *needs* to use the switch statement rather than simple Date/Time operations due to some requirement, I would tend to suspect the requirement is academic.

Third, if you need an if/else if, why not just use it, instead of hacking the switch statement to act like one?


[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Fourth, I just tried it to see if it would compile, and as I suspected it doesn't (errors with "A constant value is expected" on each of the iMth comparison). If it did compile, I think it would handle iMth values of 11 and 12 incorrectly.

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
You are testing iMth twice

Switch is an if statement equivalent.

If this code works you will only have the switch statement execute January, February and March.

Use case 1,2,3,........12 Or just if statements

Jurij
 
If you have to use a switch statement in an OO language then you probably missed a pattern or two.

It is usually refered to as a code smell.

Christiaan Baes
Belgium

My Blog
 
>Third, if you need an if/else if, why not just use it, instead of hacking the switch statement to act like one?
One has to know much more to make this kind of statement.
 
Fair enough, I guess I should have explained better. The switch statement's not designed to do anything but evaluate a value against constants, in C# anyway. The example you posted does not work in C#. Having seen several of your posts in the past I'm sure that it does work in other language(s), or you would not have posted it, I just have not tested it with any languages I'm familiar with that employ the switch statement. The example you give translates to something like this:

Code:
if (iMth == 1)
{
	//do something
}
else if (iMth > 1 && iMth < 10)
{
	//do something else
}
else if (iMth >= 10)
{
	//do yet another thing
}

These if/else-if's are what the language gives us to check if a value is within a certain range. To do the same in a switch would need to be something like this (not sure if this is right, I very rarely find occasion to use the switch statement, but I know this is the general idea):

Code:
switch (iMth)
{
	case 1:
		//do something
		break;
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
	case 7:
	case 8:
	case 9:
		//do something else
		break;
	default:
		//do the other thing
		break;
}

So, we have two different tools within the language to do essentially the same thing. Both have their advantages and disadvantages. While I find your example very creative and appreciate the thinking that went into it, I simply don't find it very helpful.

We choose to work within the confines of our various languages, and to blur the line between the switch and if statements' capabilities simply for the sake of using the swtich statement just seems irresponsible.



[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Why use switch or if statements anyway? Cool people use ternary operators.


? FTW!

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Alex, you are right, I was wrong and my layout mistaken in the language of c#. Thank you for your trouble in explaining. I appreciate it.

I wouldn't at all be trying to extrapolate one paradigm of thoughts in one language to another at any time. Each retains its characteristic and goals. But to this particular expression, I think the c# designer team has thought too much on the side-effect... so as to restrict the thing to constant-expression.

Thank you again, Alex!
 
tsuji, c# was originally designed by the person who created Delphi. Since Delphi is a variant of Pascal and the Case statement in Pascal (equivalent to switch in c-based languages) requires a constant, it is not surprising that this practice was followed.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top