954,135 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# beginner switch statement

switch(time)
{

case 1: System.Console.WriteLine(" Enter the century. ie 20 or 19.");
C = System.Console.ReadLine();
Convert.ToInt32(C);
goto case 2;


case 2: System.Console.WriteLine("Enter year in YY format. ie 96") ;
Y = System.Console.ReadLine();
Convert.ToInt32(Y);
goto case 3;


case 3: System.Console.WriteLine("Enter month in MM format. ie 03");
M = System.Console.ReadLine();
Convert.ToInt32(M);
goto case 4;
case 4: System.Console.WriteLine("Enter the two digit day of the month. ie 13");
D = System.Console.ReadLine();
Convert.ToInt32(D);
break;
}

I want this switch statemement to fall through. Whats preventing it??

* It wasnt the switch statement at all* oops

Mix
Junior Poster in Training
77 posts since Sep 2006
Reputation Points: 8
Solved Threads: 0
 

Yeah, C# doesn't support falling through on switch statements ;)

Iron_Cross
Junior Poster
117 posts since Jul 2003
Reputation Points: 46
Solved Threads: 2
 

That's one of the fallbacks of a "strong" language such as C# over a "weak" one such as C (this is not a comparing of the strength of the language but the strength of the safeguards build into the language). You might just have to use nested if blocks to accomplish your goal whereas this could easily be done in C.

Regards,

Tyler S. Breton

TylerSBreton
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 25
Solved Threads: 3
 

By the way,

C# does not allow this the way you wish to have it done, executing multiple lines of code in one case and jumping to another (unless you use proper goto statements *bad practice*, which ARE supported in C#). However, you CAN fall through one case to another if and only if the case is blank, which technically isn't "falling through" the way we think of it, but in reality just declaring more than once case for a particular chunk of code.

Example:

switch(a)
{
 
    case 1:
    case 2:
    case 3:
        x++;
        break;
    case 4:
        y++;
        break;
 
    default:
        z++;
        break;
 
}


Hope this clarifies things a bit.

Best Regards,

Tyler S. Breton

TylerSBreton
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 25
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You