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. how to use delegate to call function and introduce function as content?

how to use delegate to call function and introduce function as content?

Scheduled Pinned Locked Moved C#
tutorialquestion
23 Posts 5 Posters 2 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.
  • Richard DeemingR Richard Deeming

    Are you looking for something like this:

    static void Main()
    {
    int x = 1;
    int y = 2;
    int z = 3;

    fun1(x, () => fun2(y));
    fun1(x, () => fun3(y, z));
    

    }

    static void fun1(int x, Action call)
    {
    call();
    }

    static void fun2(int y) { ... }
    static void fun3(int y, int z) { ... }

    Lambda Expressions (C# Programming Guide)[^] C# in Depth : The Beauty of Closures[^]


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    S Offline
    S Offline
    smallkubi
    wrote on last edited by
    #8

    Thank you very much,it 's near my requirement. But one more requirement is that: static void fun1(int x, Action call) { call(); } take fun3(y,z) for example, call() is run fun3(y,z). but if i want to run fun3(x,z) ,how to realize it? you know ,i want to change y value in fun1 to call fun2(int y) or fun3(int y,int z),because my code use loop to change y value in fun1,to let fun2 or fun3 be a minimum value.

    Richard DeemingR 1 Reply Last reply
    0
    • S smallkubi

      Thank you very much,it 's near my requirement. But one more requirement is that: static void fun1(int x, Action call) { call(); } take fun3(y,z) for example, call() is run fun3(y,z). but if i want to run fun3(x,z) ,how to realize it? you know ,i want to change y value in fun1 to call fun2(int y) or fun3(int y,int z),because my code use loop to change y value in fun1,to let fun2 or fun3 be a minimum value.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #9

      So something like this:

      static void Main()
      {
      int x = 1;
      int z = 3;
      fun1(x, y => fun2(y));
      fun1(x, y => fun3(y, z));
      }

      static void fun1(int x, Action<int> call)
      {
      call(x);
      }

      static void fun2(int y) { ... }
      static void fun3(int y, int z) { ... }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      S 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        So something like this:

        static void Main()
        {
        int x = 1;
        int z = 3;
        fun1(x, y => fun2(y));
        fun1(x, y => fun3(y, z));
        }

        static void fun1(int x, Action<int> call)
        {
        call(x);
        }

        static void fun2(int y) { ... }
        static void fun3(int y, int z) { ... }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        S Offline
        S Offline
        smallkubi
        wrote on last edited by
        #10

        Great Job!!! Thank you very much. And i have other question bothering me. I have to use chart control to plot 10 series including 1000 points per series in one chartarea. But after ploting, mouse move or zoom in or ContextMenuStrip operation is very slow and delay. I think maybe paint problem. When i ask before, i get the answer is point number is too big. But i found that if i plot 200 series with 1 point for example as follows, the operation is also slow. How can treat it? follows is for example: Series series1; for(int i=0;i<200;i++) { series1=new series(); series1.Points.Add(new DataPoint(i,i)); chart1.Series.Add(series1); }

        L 1 Reply Last reply
        0
        • S smallkubi

          Great Job!!! Thank you very much. And i have other question bothering me. I have to use chart control to plot 10 series including 1000 points per series in one chartarea. But after ploting, mouse move or zoom in or ContextMenuStrip operation is very slow and delay. I think maybe paint problem. When i ask before, i get the answer is point number is too big. But i found that if i plot 200 series with 1 point for example as follows, the operation is also slow. How can treat it? follows is for example: Series series1; for(int i=0;i<200;i++) { series1=new series(); series1.Points.Add(new DataPoint(i,i)); chart1.Series.Add(series1); }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #11

          smallkubi wrote:

          I think maybe paint problem.

          Why?

          smallkubi wrote:

          When i ask before, i get the answer is point number is too big.

          The more points you need to draw, the longer it takes. If there had been a useless delay in the chart-control, people would have noticed.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          S 1 Reply Last reply
          0
          • L Lost User

            smallkubi wrote:

            I think maybe paint problem.

            Why?

            smallkubi wrote:

            When i ask before, i get the answer is point number is too big.

            The more points you need to draw, the longer it takes. If there had been a useless delay in the chart-control, people would have noticed.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

            S Offline
            S Offline
            smallkubi
            wrote on last edited by
            #12

            I mean Dalay is not that Chart generating process is slow, when i finished plot, i want to operate on Chart, like mouse moving, Scroll... is slow.

            L 1 Reply Last reply
            0
            • S smallkubi

              I mean Dalay is not that Chart generating process is slow, when i finished plot, i want to operate on Chart, like mouse moving, Scroll... is slow.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #13

              Meaning it will have to recalculate which part it should show and repaint it. That takes time. Smaller charts with less points will be faster. If you find that the Chart-control is "too slow" for your needs, you may have to implement your own.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

              S 1 Reply Last reply
              0
              • L Lost User

                Meaning it will have to recalculate which part it should show and repaint it. That takes time. Smaller charts with less points will be faster. If you find that the Chart-control is "too slow" for your needs, you may have to implement your own.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                S Offline
                S Offline
                smallkubi
                wrote on last edited by
                #14

                oh, do you know how to stop control auto refresh? i think if i can refresh it in mannual?

                L 1 Reply Last reply
                0
                • S smallkubi

                  oh, do you know how to stop control auto refresh? i think if i can refresh it in mannual?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #15

                  Stop the user from scrolling, panning and/or zooming, and you don't need to refresh. If anything changes, a refresh would be required to get the updated data on screen.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                  S 1 Reply Last reply
                  0
                  • L Lost User

                    Stop the user from scrolling, panning and/or zooming, and you don't need to refresh. If anything changes, a refresh would be required to get the updated data on screen.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                    S Offline
                    S Offline
                    smallkubi
                    wrote on last edited by
                    #16

                    Now i want to have the function of Zooming,scrolling... Anything changes, screen don't update automatically,I want to updated data on screen mannully, can it be possible?

                    L 1 Reply Last reply
                    0
                    • S smallkubi

                      Now i want to have the function of Zooming,scrolling... Anything changes, screen don't update automatically,I want to updated data on screen mannully, can it be possible?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #17

                      Sure; use the drawing functions to draw your chart. Means writing code to calculate the length of the axes, drawing each point, drawing some labels.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                      1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        So something like this:

                        static void Main()
                        {
                        int x = 1;
                        int z = 3;
                        fun1(x, y => fun2(y));
                        fun1(x, y => fun3(y, z));
                        }

                        static void fun1(int x, Action<int> call)
                        {
                        call(x);
                        }

                        static void fun2(int y) { ... }
                        static void fun3(int y, int z) { ... }


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        S Offline
                        S Offline
                        smallkubi
                        wrote on last edited by
                        #18

                        Hi, i have another question... If i want to use multi-thread to call fun1, how can i input the variable x, y,z? for example: static void Main() { int x = 1; int z = 3; Thread th=new Thread(fun1( ? )) //how can i do ? th.start(); // fun1(x, y => fun2(y)); // fun1(x, y => fun3(y, z)); } static void fun1(int x, Action call) { call(x); } static void fun2(int y) { ... } static void fun3(int y, int z) { ... }

                        Richard DeemingR 1 Reply Last reply
                        0
                        • S smallkubi

                          Hi, i have another question... If i want to use multi-thread to call fun1, how can i input the variable x, y,z? for example: static void Main() { int x = 1; int z = 3; Thread th=new Thread(fun1( ? )) //how can i do ? th.start(); // fun1(x, y => fun2(y)); // fun1(x, y => fun3(y, z)); } static void fun1(int x, Action call) { call(x); } static void fun2(int y) { ... } static void fun3(int y, int z) { ... }

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #19

                          Something like this:

                          Thread th = new Thread(() => fun1(x, y => fun3(y, z)));

                          Breaking it down:

                          Action<int> call = y => fun3(y, z);
                          ThreadStart start = () => fun1(x, call);
                          Thread th = new Thread(start);


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          S 2 Replies Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            Something like this:

                            Thread th = new Thread(() => fun1(x, y => fun3(y, z)));

                            Breaking it down:

                            Action<int> call = y => fun3(y, z);
                            ThreadStart start = () => fun1(x, call);
                            Thread th = new Thread(start);


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            S Offline
                            S Offline
                            smallkubi
                            wrote on last edited by
                            #20

                            Good Idea,i meet it. And if i have a button at main Thread , it can abort Thread "th". How to code button click function? And, if whether Thread th end or abort, i want to run" MessageBox.Show("Thread end")", how can i do it?

                            Richard DeemingR 1 Reply Last reply
                            0
                            • S smallkubi

                              Good Idea,i meet it. And if i have a button at main Thread , it can abort Thread "th". How to code button click function? And, if whether Thread th end or abort, i want to run" MessageBox.Show("Thread end")", how can i do it?

                              Richard DeemingR Offline
                              Richard DeemingR Offline
                              Richard Deeming
                              wrote on last edited by
                              #21

                              smallkubi wrote:

                              it can abort Thread "th"

                              Don't. How To Stop a Thread in .NET (and Why Thread.Abort is Evil)[^] If you're using .NET 4.5 or above, you'd probably have better luck using async and await. Asynchronous Programming with Async and Await (C# and Visual Basic)[^] Otherwise, use something like the BackgroundWorker component[^].


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                              1 Reply Last reply
                              0
                              • Richard DeemingR Richard Deeming

                                Something like this:

                                Thread th = new Thread(() => fun1(x, y => fun3(y, z)));

                                Breaking it down:

                                Action<int> call = y => fun3(y, z);
                                ThreadStart start = () => fun1(x, call);
                                Thread th = new Thread(start);


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                S Offline
                                S Offline
                                smallkubi
                                wrote on last edited by
                                #22

                                hi, If i use Func call= y=>fun3(y,z) , fun3 can be one output param function. But if my function need 2 or more output, how can i do it?

                                Richard DeemingR 1 Reply Last reply
                                0
                                • S smallkubi

                                  hi, If i use Func call= y=>fun3(y,z) , fun3 can be one output param function. But if my function need 2 or more output, how can i do it?

                                  Richard DeemingR Offline
                                  Richard DeemingR Offline
                                  Richard Deeming
                                  wrote on last edited by
                                  #23

                                  Sorry, I'm not following you. Do you mean you want multiple return values from your function? If so, you'll need to create a class or structure to contain them:

                                  public struct Fun3ReturnValues
                                  {
                                  public int SomeValue { get; set; }
                                  public string SomeOtherValue { get; set; }
                                  }
                                  ...
                                  public static Fun3ReturnValues Fun3(int y, int z)
                                  {
                                  return new Fun3ReturnValues
                                  {
                                  SomeValue = 42,
                                  SomeOtherValue = "Hello",
                                  };
                                  }

                                  Alternatively, you could use the Tuple class[^], but the meaning of the values wouldn't be as clear.


                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                  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