Share via


Globals.VariablePersists Property

The VariablePersists property applies to several types of Globals objects. For the DTE.Globals object, it gets or sets whether the variable is retained by the environment and is available between sessions of the environment. For the Solution.Globals object, it gets or sets whether the variable is retained by the environment and is available between sessions of the environment and between loading and unloading of a solution. For the Project.Globals object, it gets or sets whether the variable is retained by the environment in the project file.

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

Syntax

'Declaration
Property VariablePersists ( _
    VariableName As String _
) As Boolean
bool this[
    string VariableName
] { get; set; }
property bool VariablePersists[String^ VariableName] {
    bool get (String^ VariableName);
    void set (String^ VariableName, bool value);
}
abstract VariablePersists : 
        VariableName:string -> bool with get, set
JScript does not support indexed properties.

Parameters

  • VariableName
    Type: System.String

    Required. Represents the name of the variable to retain.

Property Value

Type: System.Boolean
A Boolean value indicating whether or not a variable exists. VariablePersists returns true if a variable exists, otherwise returns false.

Remarks

Although Global variables always persist within a session of Visual Studio, VariablePersists allows these variables to persist between sessions.

Note

To save variables with a particular solution, use DTE.Solution.Globals.

If the variable does not exist, VariablePersists returns false.

For the Solution object (Solution.Globals), data is saved whenever the solution is saved. Modifying the Globals object causes the solution file to become marked as edited (or "dirty"). For the DTE object (DTE.Globals), data is saved either when the Visual Studio environment is shut down or when a solution is saved. In both cases, the data is stored either in the solution (.sln) file or in the structured storage file in the User Profiles directory.

When the environment is shut down or a Save All occurs, all global values are saved. If VariablePersists is associated with the DTE object, the value is saved in the user options directory of the Visual Studio environment.

If the global variable is associated with the Solution object, then the value is saved in the solution (.sln) file. The values are saved any time the environment writes the .sln file.

Any saved variables overwrite previously saved values. To remove a variable from the saved file, set VariablePersists to false. The environment will remove its value during the next Save operation.

Note

VariableValue names cannot contain spaces. If one does, you get the error, Value does not fall within expected range.

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