Compartilhar via


Globals Interface

Definition

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

public interface class Globals
public interface class Globals
__interface Globals
[System.Runtime.InteropServices.Guid("E68A3E0E-B435-4DDE-86B7-F5ADEFC19DF2")]
[System.Runtime.InteropServices.TypeLibType(4160)]
public interface Globals
[<System.Runtime.InteropServices.Guid("E68A3E0E-B435-4DDE-86B7-F5ADEFC19DF2")>]
[<System.Runtime.InteropServices.TypeLibType(4160)>]
type Globals = interface
Public Interface Globals
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

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.

Note

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.

The Globals object can be used to save user-defined data unique to each user between Visual Studio sessions. You 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.

Note

VariableValue[] name strings cannot contain space, colon (:), or period(.) characters. If a name has any of these characters, you get the error, "Value does not fall within expected range."

Properties

DTE

Gets the top-level extensibility object.

Parent

Gets the immediate parent object of a Globals object.

VariableExists[String]

Returns whether the specified variable exists.

VariableNames

Gets a list of all current global variable names.

VariablePersists[String]

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.

VariableValue[String]

Returns or sets the variable with the specified name.

Applies to