how to add an optional parameter RRS feed

  • Question

  • Hello,

    I been trying to add optional parameters to a function via the method 
    EnvDTE.CodeFunction.Function AddParameter(ByVal Name As String, ByVal Type As Object, Optional ByVal Position As Object = Nothing) . After adding the parameter I cast it to EnvDTE80.CodeParameter2 and set its ParameterKind property to vsCMParameterKind.vsCMParameterKindIn + vsCMParameterKind.vsCMParameterKindOptional but it throws an exception saying "Value does not fall within the expected range."

    Thanks in advance.

    Tuesday, June 1, 2010 9:44 AM


  • In order to set the ParameterKind property to vsCMParameterKind.vsCMParameterKindOptional, you need to begin a batch edit on the FileCodeModel2 object. Try something like this:

     Dim param As CodeParameter2 = myFunc.Parameters.Item(1)
     param.ParameterKind = vsCMParameterKind.vsCMParameterKindIn + vsCMParameterKind.vsCMParameterKindOptional
     param.DefaultValue = "12"
    End Try


    Note: It's not explicitly necessary that you set the DefaultValue property here.

    Tuesday, June 1, 2010 6:30 PM