0 out of 1 rated this helpful - Rate this topic

Globals Interface

The Globals object is a cache for storing data for the duration of each session of the Visual Studio environment, as well as across sessions using the VariablePersists property.

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

[GuidAttribute("E68A3E0E-B435-4DDE-86B7-F5ADEFC19DF2")] 
public interface Globals
/** @attribute GuidAttribute("E68A3E0E-B435-4DDE-86B7-F5ADEFC19DF2") */ 
public interface Globals
GuidAttribute("E68A3E0E-B435-4DDE-86B7-F5ADEFC19DF2") 
public interface Globals

The Globals object, for example, allows programs to have global variables whose values persist between executions. It can also be used to allow a command to implement a default value if it requires the user to enter information each time it executes. Furthermore, it can be used to change its behavior after it has been invoked a certain number of times.

Data is stored in the Globals object as name/variant-value pairs. These name/value pairs can optionally be stored on disk by using the VariablePersists property to maintain their state (as a string) between different sessions of Visual Studio.

NoteNote

Variables containing objects or SafeArrays cannot be saved. If the value can be saved as a string, then it is saved in its native format.

Add-ins or macros can also use the Globals object to save user-defined data unique to each user between Visual Studio sessions. They can also use the Globals object to save data to and retrieve data from a solution (.sln) file.

Use the VariableValue property to save or read values saved with the Globals object.

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 Globalsglobals = 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.");
}
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ