Export (0) Print
Expand All

CodeLabeledStatement Class

Represents a labeled statement or a stand-alone label.

For a list of all members of this type, see CodeLabeledStatement Members.

System.Object
   System.CodeDom.CodeObject
      System.CodeDom.CodeStatement
         System.CodeDom.CodeLabeledStatement

[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDispatch)>
<ComVisible(True)>
Public Class CodeLabeledStatement
   Inherits CodeStatement
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
public class CodeLabeledStatement : CodeStatement
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDispatch)]
[ComVisible(true)]
public __gc class CodeLabeledStatement : public CodeStatement
[JScript]
public
   Serializable
 ClassInterface(ClassInterfaceType.AutoDispatch)
 ComVisible(true)
class CodeLabeledStatement extends CodeStatement

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

CodeLabeledStatement represents a label and optionally, an associated statement. A label can be used to indicate the target of a CodeGotoStatement.

The Statement property is optional. To create only a label, leave the Statement property uninitialized.

Note   Not all languages support goto statements and labels, so you should test wheter a code generator supports goto statements and labels by calling the Supports method with the GotoStatements flag.

Example

[Visual Basic, C#, C++] The following example code demonstrates use of a CodeGotoStatement and a CodeLabeledStatement to redirect program flow.

[Visual Basic] 
' Declares a type to contain the example code.
Dim type1 As New CodeTypeDeclaration("Type1")
' Declares an entry point method.
Dim entry1 As New CodeEntryPointMethod()
type1.Members.Add(entry1)
' Adds a goto statement to continue program flow at the "JumpToLabel" label.
Dim goto1 As New CodeGotoStatement("JumpToLabel")
entry1.Statements.Add(goto1)
' Invokes Console.WriteLine to print "Test Output", which is skipped by the goto statement.
Dim method1 As New CodeMethodInvokeExpression(New CodeTypeReferenceExpression("System.Console"), "WriteLine", New CodePrimitiveExpression("Test Output."))
entry1.Statements.Add(method1)
' Declares a label named "JumpToLabel" associated with a method to output a test string using Console.WriteLine.
Dim method2 As New CodeMethodInvokeExpression(New CodeTypeReferenceExpression("System.Console"), "WriteLine", New CodePrimitiveExpression("Output from labeled statement."))
Dim label1 As New CodeLabeledStatement("JumpToLabel", New CodeExpressionStatement(method2))
entry1.Statements.Add(label1)

' A Visual Basic code generator produces the following source code for the preceeding example code:

'   Public Class Type1
'
'       Public Shared Sub Main()
'           GoTo JumpToLabel
'           System.Console.WriteLine("Test Output")
'           JumpToLabel:
'           System.Console.WriteLine("Output from labeled statement.")
'       End Sub
'   End Class

[C#] 
// Declares a type to contain the example code.
CodeTypeDeclaration type1 = new CodeTypeDeclaration("Type1");            
// Declares an entry point method.
CodeEntryPointMethod entry1 = new CodeEntryPointMethod();                        
type1.Members.Add( entry1 );
// Adds a goto statement to continue program flow at the "JumpToLabel" label.
CodeGotoStatement goto1 = new CodeGotoStatement("JumpToLabel");
entry1.Statements.Add( goto1 );
// Invokes Console.WriteLine to print "Test Output", which is skipped by the goto statement.
CodeMethodInvokeExpression method1 = new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression("System.Console"), "WriteLine", new CodePrimitiveExpression("Test Output."));
entry1.Statements.Add( method1 );
// Declares a label named "JumpToLabel" associated with a method to output a test string using Console.WriteLine.
CodeMethodInvokeExpression method2 = new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression("System.Console"), "WriteLine", new CodePrimitiveExpression("Output from labeled statement."));
CodeLabeledStatement label1 = new CodeLabeledStatement("JumpToLabel", new CodeExpressionStatement(method2) );
entry1.Statements.Add( label1 );

// A C# code generator produces the following source code for the preceeding example code:

//    public class Type1 
//    {
//        
//        public static void Main() 
//        {
//            goto JumpToLabel;
//            System.Console.WriteLine("Test Output");
//            JumpToLabel:
//            System.Console.WriteLine("Output from labeled statement.");
//        }
//    }

[C++] 
// Declares a type to contain the example code.
CodeTypeDeclaration* type1 = new CodeTypeDeclaration(S"Type1");            
// Declares an entry point method.
CodeEntryPointMethod* entry1 = new CodeEntryPointMethod();                        
type1->Members->Add( entry1 );
// Adds a goto statement to continue program flow at the "JumpToLabel" label.
CodeGotoStatement* goto1 = new CodeGotoStatement(S"JumpToLabel");
entry1->Statements->Add( goto1 );
// Invokes Console.WriteLine to print "Test Output", which is skipped by the goto statement.
CodeExpression* temp[] = {new CodePrimitiveExpression(S"Test Output.")};
CodeMethodInvokeExpression* method1 = new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression(S"System.Console"), S"WriteLine", temp);
entry1->Statements->Add( method1 );
// Declares a label named "JumpToLabel" associated with a method to output a test string using Console.WriteLine.
CodeExpression* temp2[] = {new CodePrimitiveExpression(S"Output from labeled statement.")};
CodeMethodInvokeExpression* method2 = new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression(S"System.Console"), S"WriteLine", temp2);
CodeLabeledStatement* label1 = new CodeLabeledStatement(S"JumpToLabel", new CodeExpressionStatement(method2) );
entry1->Statements->Add( label1 );

// A C# code generator produces the following source code for the preceeding example code:

//    public class Type1 
//    {
//        
//        public static void Main() 
//        {
//            goto JumpToLabel;
//            System.Console.WriteLine("Test Output");
//            JumpToLabel:
//            System.Console.WriteLine("Output from labeled statement.");
//        }
//    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.CodeDom

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

CodeLabeledStatement Members | System.CodeDom Namespace | CodeGotoStatement

Show:
© 2014 Microsoft