Partager via


CodeVariable.InitExpression Property

Sets or gets an object defining the initialization code for an element.

Namespace:  EnvDTE
Assembly:  EnvDTE (in EnvDTE.dll)

Syntax

'Declaration
Property InitExpression As Object
Object InitExpression { get; set; }
property Object^ InitExpression {
    Object^ get ();
    void set (Object^ value);
}
abstract InitExpression : Object with get, set
function get InitExpression () : Object 
function set InitExpression (value : Object)

Property Value

Type: System.Object
An object defining the initialization expression for the code variable.

Remarks

The value must be a string or a CodeElement for an expression object. When setting this to a string, the implementation of the property inserts any required syntax, such as equal signs or semicolons, if the variable does not already have an initialization expression.

Depending on the languages and syntactic or semantic checks it performs on the passed-in string, setting this property might fail. Languages are not required to check the string, and because the string is necessarily language-dependent, setting this property can result in undefined behavior if the string has ill-formed content.

When setting this property to a CodeElement, whether or not the CodeElement must be newly created depends on the language implementation of the code model. Some languages might implement copying semantics if you pass in a CodeElement that is already in a source file.

Note

The values of code model elements such as classes, structs, functions, attributes, delegates, and so forth can be non-deterministic after making certain kinds of edits, meaning that their values cannot be relied upon to always remain the same. For more information, see the section Code Model Element Values Can Change in Discovering Code by Using the Code Model (Visual Basic).

Examples

Sub InitExpressionExample(ByVal dte As DTE2)

    ' Before running this example, open a code document from a project
    ' and place the insertion point inside a class definition.
    Try
        ' Retrieve the CodeClass at the insertion point.
        Dim sel As TextSelection = _
            CType(dte.ActiveDocument.Selection, TextSelection)
        Dim cls As CodeClass = _
            CType(sel.ActivePoint.CodeElement( _
            vsCMElement.vsCMElementClass), CodeClass)

        ' Create and initialize a new member variable.
        Dim byt As CodeVariable = _
            cls.AddVariable("var1", vsCMTypeRef.vsCMTypeRefByte)
        byt.InitExpression = "255"
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
public void InitExpressionExample(DTE2 dte)
{
    // Before running this example, open a code document from a project
    // and place the insertion point inside a class definition.
    try
    {
        // Retrieve the CodeClass at the insertion point.
        TextSelection sel = 
            (TextSelection)dte.ActiveDocument.Selection;
        CodeClass cls = 
            (CodeClass)sel.ActivePoint.get_CodeElement(
            vsCMElement.vsCMElementClass);

        // Create and initialize a new member variable.
        CodeVariable byt = cls.AddVariable("var1", 
            vsCMTypeRef.vsCMTypeRefByte, -1, 
            vsCMAccess.vsCMAccessPublic, null);
        byt.InitExpression = "255";
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

.NET Framework Security

See Also

Reference

CodeVariable Interface

EnvDTE Namespace

Other Resources

How to: Compile and Run the Automation Object Model Code Examples

Discovering Code by Using the Code Model (Visual Basic)

Discovering Code by Using the Code Model (Visual C#)