Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Call one function from another

Call one function from another

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrei Ungureanu
    wrote on last edited by
    #1

    Hi, How can i call a function from another when i only have the name of the function in a string parameter. I want to call the second function with the designated parameters and get the return value. public double F2(string param1, int param2) { } public void F1(string FunctionName) { //... // here i want to call F2 using FunctionName parameter, set the F2's parameters and get the return value } Please help :)

    Do your best to be the best

    A K G 3 Replies Last reply
    0
    • A Andrei Ungureanu

      Hi, How can i call a function from another when i only have the name of the function in a string parameter. I want to call the second function with the designated parameters and get the return value. public double F2(string param1, int param2) { } public void F1(string FunctionName) { //... // here i want to call F2 using FunctionName parameter, set the F2's parameters and get the return value } Please help :)

      Do your best to be the best

      A Offline
      A Offline
      Amar Chaudhary
      wrote on last edited by
      #2

      double res;
      res = F2("",0);

      or

      F2("",0);

      1 Reply Last reply
      0
      • A Andrei Ungureanu

        Hi, How can i call a function from another when i only have the name of the function in a string parameter. I want to call the second function with the designated parameters and get the return value. public double F2(string param1, int param2) { } public void F1(string FunctionName) { //... // here i want to call F2 using FunctionName parameter, set the F2's parameters and get the return value } Please help :)

        Do your best to be the best

        K Offline
        K Offline
        Karthik Kalyanasundaram
        wrote on last edited by
        #3

        You need to use Reflection here. The following code is not complete but I think it will help you. using System.Reflection; ..... public void CallMethod(string methodName) { MethodInfo methodInfo = this.GetType().GetMethod(methodName); if (methodInfo == null) { throw new MissingMethodException(this.GetType().Name,methodName); } methodInfo.Invoke(this, new object[] { "Param1", 50 }); } public void MethodToBeCalled(string param1, int v) { Console.WriteLine(param1); Console.WriteLine(v); }

        1 Reply Last reply
        0
        • A Andrei Ungureanu

          Hi, How can i call a function from another when i only have the name of the function in a string parameter. I want to call the second function with the designated parameters and get the return value. public double F2(string param1, int param2) { } public void F1(string FunctionName) { //... // here i want to call F2 using FunctionName parameter, set the F2's parameters and get the return value } Please help :)

          Do your best to be the best

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Why, oh why, would you identify the method using a string? Perhaps you should explain what it is that you are trying to accomplish, instead of asking how it's done the way that you think that it should be done.

          --- b { font-weight: normal; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups