Compartilhar via


Globals.VariablePersists[String] Property

Definition

The VariablePersists[String] 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.

public:
 property bool VariablePersists[System::String ^] { bool get(System::String ^ VariableName); void set(System::String ^ VariableName, bool value); };
[System.Runtime.InteropServices.DispId(4)]
public bool VariablePersists[string VariableName] { [System.Runtime.InteropServices.DispId(4)] get; [System.Runtime.InteropServices.DispId(4)] set; }
[<System.Runtime.InteropServices.DispId(4)>]
[<get: System.Runtime.InteropServices.DispId(4)>]
[<set: System.Runtime.InteropServices.DispId(4)>]
member this.VariablePersists(string) : bool with get, set
Public Property VariablePersists(VariableName As String) As Boolean

Parameters

VariableName
String

Required. Represents the name of the variable to retain.

Property Value

A Boolean value indicating whether or not a variable exists. VariablePersists[String] returns true if a variable exists, otherwise returns false.

Attributes

Examples

Sub GlobalsExample(ByVal dte As DTE)  
    Dim globals As Globals  
    globals = dte.Solution.Globals  
    If globals.VariableExists("GlobalsExample") Then  
        ' The counter has already been set, so increment it.  
        Dim int32 As System.Int32  
        int32 = System.Int32.Parse(CStr(globals("GlobalsExample")))  
        int32 += 1  
        globals("GlobalsExample") = int32.ToString()  
    Else  
        ' Counter has never been set, so create and initialize it.  
        globals("GlobalsExample") = 1.ToString()  
        globals.VariablePersists("GlobalsExample") = True  
    End If  
    MsgBox("This variable has been called: " & _  
    globals.VariableValue("GlobalsExample") & " times.")  
End Sub  
void GlobalsExample(_DTE applicationObject)  
{  
    Globals globals;  
    globals = applicationObject.Solution.Globals;  
    if(globals.get_VariableExists("GlobalsExample"))  
    {  
        // The counter has already been set, so increment it.  
        System.Int32 int32;  
        int32 = System.Int32.Parse((string)  
        globals["GlobalsExample"]);  
        int32++;  
        globals["GlobalsExample"] = int32.ToString();  
    }  
    else  
    {  
        // Counter has never been set, so create and initialize it.  
        globals["GlobalsExample"] = 1.ToString();  
        globals.set_VariablePersists("GlobalsExample", true);  
    }  
    System.Windows.Forms.MessageBox.Show("This variable has been called:   
    " + globals.VariableValue["GlobalsExample"] + " times.");  
}  

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.

Applies to