Globals.VariablePersists Property
Assembly: EnvDTE (in envdte.dll)
/** @property */ boolean get_VariablePersists (String VariableName) /** @property */ void set_VariablePersists (String VariableName, /** @attribute InAttribute() */ boolean pVal)
Parameters
- VariableName
Required. Represents the name of the variable to retain.
Property Value
A Boolean value indicating whether or not a variable exists. VariablePersists returns true if a variable exists, otherwise returns false.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. |
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."); }
Note