Share via


Globals.VariableExists Property

Returns whether the specified variable exists.

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

Syntax

'Declaration
ReadOnly Property VariableExists ( _
    Name As String _
) As Boolean
bool this[
    string Name
] { get; }
property bool VariableExists[String^ Name] {
    bool get (String^ Name);
}
abstract VariableExists : bool
JScript does not support indexed properties.

Parameters

  • Name
    Type: System.String
    Required. Represents the name of the variable.

Property Value

Type: System.Boolean
A Boolean value indicating true if the variable exists, false if it does not.

Remarks

If you attempt to check the value of a variable with the VariableValue property and the variable does not exist, a new variable of that name is created with a null value. To distinguish between an empty variable and a nonexistent variable, use the VariableExists property.

Variables:

  • Have no limit as to length, other than system limitations.

  • Are case-insensitive.

  • Can contain any characters permitted by the system.

  • Are restricted to simple data types such as strings and numbers. No SafeArrays or IDispatch interfaces can be used.

Examples

Sub OnAddinLoaded(ByVal dte As DTE)
    ' Count the number of times an add-in is loaded
    ' and store the value in the solution.
    Dim globals As Globals
    globals = dte.Solution.Globals
    If globals.VariableExists("AddinLoadCounter") Then
        ' The counter has already been set, so increment it.
        Dim int32 As System.Int32
        int32 = System.Int32.Parse(CStr(globals("AddinLoadCounter")))
        int32 += 1
        globals("AddinLoadCounter") = int32.ToString()
    Else
        ' Counter has never been set, so create and initialize it.
        globals("AddinLoadCounter") = 1.ToString()
        globals.VariablePersists("AddinLoadCounter") = True
    End If
    MsgBox("This add-in has been loaded: " & _
    globals.VariableValue("AddinLoadCounter") & " times.")
End Sub
void OnAddinLoaded(_DTE applicationObject)
{
    // Count the number of times an add-in is loaded
    // and store the value in the solution.
    Globals globals;
    globals = applicationObject.Solution.Globals;
    if(globals.get_VariableExists("AddinLoadCounter"))
    {
        // The counter has already been set, so increment it.
        System.Int32 int32;
        int32 = System.Int32.Parse((string)
        globals["AddinLoadCounter"]);
        int32++;
        globals["AddinLoadCounter"] = int32.ToString();
    }
    else
    {
        // Counter has never been set, so create and initialize it.
        globals["AddinLoadCounter"] = 1.ToString();
        globals.set_VariablePersists("AddinLoadCounter", true);
    }
    System.Windows.Forms.MessageBox.Show("This add-in has been loaded: 
    " + globals.VariableValue["AddinLoadCounter"] + " times.");
}

.NET Framework Security

See Also

Reference

Globals Interface

EnvDTE Namespace

Other Resources

Persisting Information in Projects and Solutions

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