EnvDTE in c#: how do i add parameters to a generated c++ function?

93 Views Asked by At

I am trying to generate a function with parameters. adding the function header and body works fine, but when I try to add parameters I get an exception that I haven't been able to solve.

func.AddParameter("num", "int");

I get the exception:

Failed to return new Code Element. Possibly syntax error. New Element Name: num

this is how I generated the function head and body:

VCCodeFunction func = (VCCodeFunction)(((VCFileCodeModel)(file.FileCodeModel)).AddFunction("testfunc", vsCMFunction.vsCMFunctionFunction, "testClass2"));
func.BodyText = "for(int i=0; i < 5; ++i){}\n return 1;";

this generates:

testClass2 testfunc()
{
    for (int i = 0; i < 5; i++) {}
    return 1;
}

as a temporary solution I get around the issue by manually adding the parameters one character left from where the body starts (or within the parenthesis)

var tp = func.GetStartPoint(vsCMPart.vsCMPartBodyWithDelimiter);
var ep = tp.CreateEditPoint();
ep.CharLeft();
ep.Insert("int num");

but I feel this is bad practice. what am I doing wrong?

0

There are 0 best solutions below