943,649 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 26063
  • C# RSS
Jan 14th, 2007
0

C# beginner switch statement

Expand Post »
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.
Similar Threads
Mix
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
Mix is offline Offline
76 posts
since Sep 2006
Jan 14th, 2007
0

Re: C# beginner switch statement

Yeah, C# doesn't support falling through on switch statements
Reputation Points: 46
Solved Threads: 2
Junior Poster
Iron_Cross is offline Offline
117 posts
since Jul 2003
Jan 15th, 2007
0

Re: C# beginner switch statement

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
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Jan 17th, 2007
0

Re: C# beginner switch statement

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:

c# Syntax (Toggle Plain Text)
  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
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: Need some help with a comboBox control
Next Thread in C# Forum Timeline: Formatting Strings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC