| | |
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
{
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 3: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 9: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
Views: 21607 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access algorithm application array asp.net audio bitmap box broadcast button c# check checkbox class code color combobox connectionproblem control conversion csharp custom data database datagrid datagridview dataset datatable degrees display dll drawing event excel exception file form format forms function game gcd gdi+ graphics grid image index input install instance internet key list listbox login loop math multithreading mysql notepad office operator picturebox prime print procedure programming query reflection remote remoting resource round saving search sendkeys server sleep socket sql statistics string text textbox threading timer update url validation vc++ view visual webbrowser windows winforms wpf write xml




