CodeEnum.AddMember Method
Creates a new member code construct and inserts the code in the correct location.
Namespace: EnvDTE
Assembly: EnvDTE (in EnvDTE.dll)
Syntax
'Declaration
Function AddMember ( _
Name As String, _
Value As Object, _
Position As Object _
) As CodeVariable
CodeVariable AddMember(
string Name,
Object Value,
Object Position
)
CodeVariable^ AddMember(
String^ Name,
Object^ Value,
Object^ Position
)
abstract AddMember :
Name:string *
Value:Object *
Position:Object -> CodeVariable
function AddMember(
Name : String,
Value : Object,
Position : Object
) : CodeVariable
Parameters
Name
Type: System.StringRequired. The name of the new member.
Value
Type: System.ObjectOptional. The InitExpression of the returned CodeVariable object. It can be passed in either as a string or as an expression object.
Position
Type: System.ObjectOptional. Default = 0. The code element after which to add the new element. If the value is a CodeElement, then the new element is added immediately after it.
If the value is a Long data type, then AddMember indicates the element after which to add the new element.
Because collections begin their count at 1, passing 0 indicates that the new element should be placed at the beginning of the collection. A value of -1 means the element should be placed at the end.
Return Value
Type: EnvDTE.CodeVariable
A CodeVariable object.
Remarks
AddMember adds a member to the enumeration and returns a CodeVariable object with IsConstant set to true.
When setting Value to a string, AddMember inserts any required syntax, such as equal signs or semicolons, if the variable does not already have an initialization expression. Depending on the languages and syntactic or semantic checks it performs on the passed-in string, setting this argument may fail. Languages are not required to check the string, and because the string is necessarily language-dependent, setting this argument could result in undefined behavior if the string has any ill-formed content.
When setting Value to a CodeElement, whether the CodeElement must be newly created depends on the language implementation of the code model. Some languages may implement copying semantics if you pass in a code element that is already in a source file.
The correctness of the arguments is determined by the language behind the code model.
Note
The values of code model elements, such as classes, structures, functions, attributes, delegates, cannot be relied upon to remain the same at all times. For more information, see the section Code Model Element Values Can Change in Discovering Code by Using the Code Model (Visual Basic).
Examples
Sub AddMemberExample(ByVal dte As DTE2)
' Before running this example, open a code document from a project
' and place the insertion point inside an enumeration.
Try
' Retrieve the CodeEnum at the insertion point.
Dim sel As TextSelection = _
CType(dte.ActiveDocument.Selection, TextSelection)
Dim enm As CodeEnum = _
CType(sel.ActivePoint.CodeElement( _
vsCMElement.vsCMElementEnum), CodeEnum)
' Add a member to the enumeration.
enm.AddMember("TestMember")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
public void AddMemberExample(DTE2 dte)
{
// Before running this example, open a code document from a project
// and place the insertion point inside an enumeration.
try
{
// Retrieve the CodeEnum at the insertion point.
TextSelection sel =
(TextSelection)dte.ActiveDocument.Selection;
CodeEnum enm =
(CodeEnum)sel.ActivePoint.get_CodeElement(
vsCMElement.vsCMElementEnum);
// Add a member to the enumeration.
enm.AddMember("TestMember", null, -1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
.NET Framework Security
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
See Also
Reference
Other Resources
How to: Compile and Run the Automation Object Model Code Examples