| | |
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 |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom customactiondata database datagrid datagridview dataset datastructure date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum event excel file filename files form format forms function gdi+ gis gtk hash httpwebrequest image index input install java label list listbox mandelbrot math mouseclick mysql operator outlook2003 path photoshop picturebox pixelinversion pixelminversion post programming radians regex remoting richtextbox server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser webcam wia windows winforms wpf xml





