locked
How to create a method with parameters? RRS feed

  • Question

  • I want to create a method object of a class with the class CodeMemberMethod,

    This method may need serveral parameters, the parameters may be simple data types like Int, bool, or may be objects of some classes like DataTable.

    How to create a method with parameters?

     

     

     

     

    Wednesday, April 19, 2006 1:38 AM

Answers

  • Hi,



    // Create an instance
    CodeMemberMethod m = new CodeMemberMethod();

    // Set the name of the member method
    m.Name = "MyFunction";

    // If there is a return type, specify it
    m.ReturnType = new CodeTypeReference("System.String");

    // Add the parameters
    m.Parameters.Add(
      new CodeParameterDeclarationExpression(
         "System.Int32",      // Parameter type
         "MyParam"            // Parameter name
      )
    );

    // Finally, add the method's statements
    m.Statements.Add(
       new CodeMethodReturnStatement(
            new CodeArgumentReferenceExpression ("blah") )
    );

     


    The above code will generate:



    string MyFunction(int MyParam)
    {
       return "blah";
    }

     


    Hope this helps,

    -chris

    Wednesday, April 19, 2006 6:21 AM

All replies

  • I'm not sure how to answer this.  I'm guessing you've found some wizard to add methods ? I'm sure such a wizard exists, however in many years of programming ( longer than .NET has been around, that's for sure ), I've only briefly used such a wizard when I started with ATL ( where I needed to add code in three places, and it was new to me ).  You add methods by typed them in to your IDE.  You can put any parameters you like in there.  The less you rely on wizards, the more likely it is that you're a progammer :-)

     

    Wednesday, April 19, 2006 3:37 AM
  • Thank you, cgraus,

    I guess maybe I didn't express myself clear.

    It is My program that generates some C# codes automatically,

    it uses the class System.CodeDom.CodeMemberMethod to describe a method.

    I want to know How to create a method with parameters dynamically in the automatically generated code.

     

     

    Wednesday, April 19, 2006 6:03 AM
  • Oh, my apologies.  *blush*

     

    Wednesday, April 19, 2006 6:04 AM
  • Hi,



    // Create an instance
    CodeMemberMethod m = new CodeMemberMethod();

    // Set the name of the member method
    m.Name = "MyFunction";

    // If there is a return type, specify it
    m.ReturnType = new CodeTypeReference("System.String");

    // Add the parameters
    m.Parameters.Add(
      new CodeParameterDeclarationExpression(
         "System.Int32",      // Parameter type
         "MyParam"            // Parameter name
      )
    );

    // Finally, add the method's statements
    m.Statements.Add(
       new CodeMethodReturnStatement(
            new CodeArgumentReferenceExpression ("blah") )
    );

     


    The above code will generate:



    string MyFunction(int MyParam)
    {
       return "blah";
    }

     


    Hope this helps,

    -chris

    Wednesday, April 19, 2006 6:21 AM
  • Chris, thank you very much for your really helpful suggestion~!

    The parameters for the method are given dynamically, the following snippet is my idea of setting parameters

    protected CodeMemberMethod CreateMethodWithParameters(..., DataTable dtParaTable)

    {

    ...

       foreach(DataRow drPara in dtParaTable.Rows)
       {
        objMethod.Parameters.Add(new CodeParameterDeclarationExpression(dr["Type"].ToString(),dr["Name"].ToString()));    
       }

    ...

    }

    Do you have any better opinions? THX.

     

     

     

    Thursday, April 20, 2006 2:11 AM
  • You can repeat this line:



    // Add the parameters
    m.Parameters.Add(
      new CodeParameterDeclarationExpression(
         "System.Int32",      // Parameter type
         "MyParam"            // Parameter name
      )
    );


     


    For as many dynamic parameters there is. Set the type and name for each parameter declarations.

    Regards,

    -chris
    Thursday, April 20, 2006 10:47 AM