Partilhar via


CodeModel2.AddFunction Method

Definition

Creates a new function code construct and inserts the code in the correct location.

EnvDTE::CodeFunction AddFunction(std::wstring const & Name, winrt::Windows::Foundation::IInspectable const & Location, EnvDTE::vsCMFunction Kind, winrt::Windows::Foundation::IInspectable const & Type, winrt::Windows::Foundation::IInspectable const & Position, EnvDTE::vsCMAccess Access = EnvDTE.vsCMAccess.vsCMAccessDefault);
[System.Runtime.InteropServices.DispId(9)]
public EnvDTE.CodeFunction AddFunction (string Name, object Location, EnvDTE.vsCMFunction Kind, object Type, object Position, EnvDTE.vsCMAccess Access = EnvDTE.vsCMAccess.vsCMAccessDefault);
[<System.Runtime.InteropServices.DispId(9)>]
abstract member AddFunction : string * obj * EnvDTE.vsCMFunction * obj * obj * EnvDTE.vsCMAccess -> EnvDTE.CodeFunction
Public Function AddFunction (Name As String, Location As Object, Kind As vsCMFunction, Type As Object, Optional Position As Object, Optional Access As vsCMAccess = EnvDTE.vsCMAccess.vsCMAccessDefault) As CodeFunction

Parameters

Name
String

Required. The name of the new function.

Location
Object

Required. The path and file name for the new function definition. Depending on the language, the file name is either relative or absolute to the project file. The file is added to the project if it is not already a project item. If the file cannot be created and added to the project, then AddFunction(String, Object, vsCMFunction, Object, Object, vsCMAccess) fails.

Kind
vsCMFunction

Required. The vsCMFunction constant indicating the type of function, such as whether the function is a property-get, a method, and so forth.

Type
Object

Required. A vsCMTypeRef constant indicating the data type that the function returns. This can be a CodeTypeRef object, a vsCMTypeRef constant, or a fully qualified type name.

Position
Object

Optional. 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 AddFunction(String, Object, vsCMFunction, Object, Object, vsCMAccess) 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.

Access
vsCMAccess

Optional. A vsCMAccess constant.

Returns

A CodeFunction object.

Implements

Attributes

Examples

Sub AddFunctionExample(ByVal dte As DTE2)  

    ' Before running this example, open a code document from a project.  
    Try  
        Dim projItem As ProjectItem = dte.ActiveDocument.ProjectItem  
        Dim cm As CodeModel = projItem.ContainingProject.CodeModel  

        ' Create a new function.  
        cm.AddFunction("TestFunction", projItem.Name, _  
            vsCMFunction.vsCMFunctionFunction, _  
            vsCMTypeRef.vsCMTypeRefInt)  
    Catch ex As Exception  
        MsgBox(ex.Message)  
    End Try  

End Sub  
public void AddFunctionExample(DTE2 dte)  
{  
    // Before running this example, open a code document from   
    // a project.  
    try  
    {  
        ProjectItem projItem = dte.ActiveDocument.ProjectItem;  
        CodeModel cm = projItem.ContainingProject.CodeModel;  

        // Create a new function.  
        cm.AddFunction("TestFunction", projItem.Name, _  
            vsCMFunction.vsCMFunctionFunction, _  
            vsCMTypeRef.vsCMTypeRefInt, -1, _  
            vsCMAccess.vsCMAccessDefault);  
    }  
    catch (Exception ex)  
    {  
        MessageBox.Show(ex.Message);  
    }  
}  

Remarks

You might add an overload operator (using vsCMFunctionOperator) as in the following example.

Sub testAddOverloadOperatorCPP()

Dim fcm As FileCodeModel = _

DTE.ActiveDocument.ProjectItem.FileCodeModel

Dim cc As CodeClass = fcm.CodeElements.Item("someClassName")

cc.AddFunction("+", vsCMFunction.vsCMFunctionOperator, _

vsCMTypeRef.vsCMTypeRefInt)

'cc.AddFunction("someFunction +", _

vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefInt)

End Sub

In this case, you must explicitly specify the name of the function you want to overload (in this example, "someFunction") in the call to AddFunction, not just the overload operator itself. Using the above code as an example,

cc.AddFunction("+", vsCMFunction.vsCMFunctionOperator,...)

does not work. You must instead use

cc.AddFunction("someFunction +", vsCMFunction.vsCMFunctionFunction,...)

Only Visual C++ implements this method because C#, Visual Basic, and J# do not allow top-level functions.

Native Visual C++ requires the colon-separated (::) format for its fully qualified type names.

Note

The values of code model elements such as classes, structs, functions, attributes, delegates, and so forth can be non-deterministic after making certain kinds of edits, meaning that their values cannot be relied upon to always remain the same.

Applies to