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. Create a delegate at run time

Create a delegate at run time

Scheduled Pinned Locked Moved C#
tutorialquestionhelp
2 Posts 2 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.
  • B Offline
    B Offline
    bonzaiholding
    wrote on last edited by
    #1

    Hi, I want to let the user to insert a string that will create a function. I know how to create a class or method at runtime. I have a delegate and I want the function that the user been created will be stored in the delegate event that i use. How can I do it? Specification The user can select the sorted function that he want's(Sort of some system class). He gets a list with this functions for example: 1. Area 2. Length 3. Custom If the user choose Area/length then the list of item that I have (very complex system) will be sorted by Area/Length but if the user choose 'Custom' then I want to show him a new Textbox and then he can write 'Area*Length +COS(Length)'. This is my delegate:

    delegate double GetGrade(SystemD SD);
    GetGrade MyGrade;

    1. Area:

    MyGrade = new GetGrade(AreaGrade);
    public double AreaGrade(SystemD SD)
    {
    return SD.Area;
    }

    2. Length:

    MyGrade = new GetGrade(LengthGrade);
    public double LengthGrade(SystemD SD)
    {
    return SD.Length;
    }

    3. Custom:

    MyGrade = new GetGrade(CustomGrade);
    public double CustomGrade(SystemD SD)
    {
    string str= CustomTextBox.Text;
    double Grade= CompileAndGetDoubleNumber(str,SD); //This is the function that i want to implement.
    // The function that i want gets the string that contain the code for calculation, and SD that represent the system(class instance). It's return the wanted value after the calculation, for example from the string '2*SD.Length+SD.Area' i will get a double that represent the value (2*Length +Area) of SD.
    return Grade;
    }

    I want to get the string from the user and create at runtime a function that can be overload to this event but I need this function before to compilation because then I can not choose the function for the event because the function does not exists. What can I do to resolve this problem?

    N 1 Reply Last reply
    0
    • B bonzaiholding

      Hi, I want to let the user to insert a string that will create a function. I know how to create a class or method at runtime. I have a delegate and I want the function that the user been created will be stored in the delegate event that i use. How can I do it? Specification The user can select the sorted function that he want's(Sort of some system class). He gets a list with this functions for example: 1. Area 2. Length 3. Custom If the user choose Area/length then the list of item that I have (very complex system) will be sorted by Area/Length but if the user choose 'Custom' then I want to show him a new Textbox and then he can write 'Area*Length +COS(Length)'. This is my delegate:

      delegate double GetGrade(SystemD SD);
      GetGrade MyGrade;

      1. Area:

      MyGrade = new GetGrade(AreaGrade);
      public double AreaGrade(SystemD SD)
      {
      return SD.Area;
      }

      2. Length:

      MyGrade = new GetGrade(LengthGrade);
      public double LengthGrade(SystemD SD)
      {
      return SD.Length;
      }

      3. Custom:

      MyGrade = new GetGrade(CustomGrade);
      public double CustomGrade(SystemD SD)
      {
      string str= CustomTextBox.Text;
      double Grade= CompileAndGetDoubleNumber(str,SD); //This is the function that i want to implement.
      // The function that i want gets the string that contain the code for calculation, and SD that represent the system(class instance). It's return the wanted value after the calculation, for example from the string '2*SD.Length+SD.Area' i will get a double that represent the value (2*Length +Area) of SD.
      return Grade;
      }

      I want to get the string from the user and create at runtime a function that can be overload to this event but I need this function before to compilation because then I can not choose the function for the event because the function does not exists. What can I do to resolve this problem?

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Perhaps Action[^] or Func[^] is what you need


      only two letters away from being an asset

      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