C# beginner switch statement

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 76
Reputation: Mix is an unknown quantity at this point 
Solved Threads: 0
Mix's Avatar
Mix Mix is offline Offline
Junior Poster in Training

C# beginner switch statement

 
0
  #1
Jan 14th, 2007
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
Last edited by Mix; Jan 14th, 2007 at 4:37 am.
.Just dancing.
Reply With Quote Quick reply to this message  
Join Date: Jul 2003
Posts: 117
Reputation: Iron_Cross is an unknown quantity at this point 
Solved Threads: 2
Iron_Cross's Avatar
Iron_Cross Iron_Cross is offline Offline
Junior Poster

Re: C# beginner switch statement

 
0
  #2
Jan 14th, 2007
Yeah, C# doesn't support falling through on switch statements
elitehackers.info
Today's Penny-Arcade!
Pain is weakness leaving the body!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: C# beginner switch statement

 
0
  #3
Jan 15th, 2007
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
Western New England College '08
Computer Science

Programming Lang's:

C, C#, Java, Lisp, MASM, Visual Basic 6
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: C# beginner switch statement

 
0
  #4
Jan 17th, 2007
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:

  1.  
  2. switch(a)
  3. {
  4.  
  5. case 1:
  6. case 2:
  7. case 3:
  8. x++;
  9. break;
  10. case 4:
  11. y++;
  12. break;
  13.  
  14. default:
  15. z++;
  16. break;
  17.  
  18. }

Hope this clarifies things a bit.

Best Regards,

Tyler S. Breton
Last edited by TylerSBreton; Jan 17th, 2007 at 10:32 pm. Reason: Code-aligning
Western New England College '08
Computer Science

Programming Lang's:

C, C#, Java, Lisp, MASM, Visual Basic 6
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC