| | |
C# beginner switch statement
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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
{
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.
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
Regards,
Tyler S. Breton
Western New England College '08
Computer Science
Programming Lang's:
C, C#, Java, Lisp, MASM, Visual Basic 6
Computer Science
Programming Lang's:
C, C#, Java, Lisp, MASM, Visual Basic 6
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:
Hope this clarifies things a bit.
Best Regards,
Tyler S. Breton
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)
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
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
Computer Science
Programming Lang's:
C, C#, Java, Lisp, MASM, Visual Basic 6
![]() |
Similar Threads
- switch statement on String in Java (Java)
- switch statement (C)
- Switch Statement, Fall-Through. (C)
- Problems with switch statement (C++)
- Problems with switch statement (C++)
Other Threads in the C# Forum
- Previous Thread: Need some help with a comboBox control
- Next Thread: Formatting Strings
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





