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. Getting null event in delegate

Getting null event in delegate

Scheduled Pinned Locked Moved C#
question
7 Posts 6 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.
  • S Offline
    S Offline
    salon
    wrote on last edited by
    #1

    Hi all, I want to raise an event with delegates my code is like this: delcaration:

    public delegate void ButtonClickedDelegate(string path);
    public event ButtonClickedDelegate ButtonClicked;

    call:

    GetData(path);

    raise:

    private void GetData(string path)
    {
    try
    {
    if (ButtonClicked!= null)
    ButtonClicked(path);
    }
    catch
    {
    }
    }

    But I am getting ButtonClicked = null at the line

    if (ButtonClicked!= null)

    so that

    ButtonClicked(path);

    is not getting executed.... Do anybody have idea what is the wrong with the code?

    P L A D 4 Replies Last reply
    0
    • S salon

      Hi all, I want to raise an event with delegates my code is like this: delcaration:

      public delegate void ButtonClickedDelegate(string path);
      public event ButtonClickedDelegate ButtonClicked;

      call:

      GetData(path);

      raise:

      private void GetData(string path)
      {
      try
      {
      if (ButtonClicked!= null)
      ButtonClicked(path);
      }
      catch
      {
      }
      }

      But I am getting ButtonClicked = null at the line

      if (ButtonClicked!= null)

      so that

      ButtonClicked(path);

      is not getting executed.... Do anybody have idea what is the wrong with the code?

      P Offline
      P Offline
      PandemoniumPasha
      wrote on last edited by
      #2

      That's because you've just declared a event handler, not created it. try something like:

      ButtonClicked += new System.EventHandler(GetData);

      regards :)

      1 Reply Last reply
      0
      • S salon

        Hi all, I want to raise an event with delegates my code is like this: delcaration:

        public delegate void ButtonClickedDelegate(string path);
        public event ButtonClickedDelegate ButtonClicked;

        call:

        GetData(path);

        raise:

        private void GetData(string path)
        {
        try
        {
        if (ButtonClicked!= null)
        ButtonClicked(path);
        }
        catch
        {
        }
        }

        But I am getting ButtonClicked = null at the line

        if (ButtonClicked!= null)

        so that

        ButtonClicked(path);

        is not getting executed.... Do anybody have idea what is the wrong with the code?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, you need to wire a handler to your event, as in ButtonClicked += new ButtonClickedDelegate(handler); Compare with what Visual Designer adds to myForm.designer.cs when you wire a handler interactively. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


        S 1 Reply Last reply
        0
        • S salon

          Hi all, I want to raise an event with delegates my code is like this: delcaration:

          public delegate void ButtonClickedDelegate(string path);
          public event ButtonClickedDelegate ButtonClicked;

          call:

          GetData(path);

          raise:

          private void GetData(string path)
          {
          try
          {
          if (ButtonClicked!= null)
          ButtonClicked(path);
          }
          catch
          {
          }
          }

          But I am getting ButtonClicked = null at the line

          if (ButtonClicked!= null)

          so that

          ButtonClicked(path);

          is not getting executed.... Do anybody have idea what is the wrong with the code?

          A Offline
          A Offline
          Arjun Marwaha
          wrote on last edited by
          #4

          Where is the handler to the event? Guess you are missing that.

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, you need to wire a handler to your event, as in ButtonClicked += new ButtonClickedDelegate(handler); Compare with what Visual Designer adds to myForm.designer.cs when you wire a handler interactively. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


            S Offline
            S Offline
            salon
            wrote on last edited by
            #5

            Getting 'System.StackOverflowException' at line

            void OnButtonClicked()

            {}

            when I do

            ButtonClicked += OnButtonClicked;

            ...

            void OnButtonClicked(...) {
            ...
            }

            M 1 Reply Last reply
            0
            • S salon

              Getting 'System.StackOverflowException' at line

              void OnButtonClicked()

              {}

              when I do

              ButtonClicked += OnButtonClicked;

              ...

              void OnButtonClicked(...) {
              ...
              }

              M Offline
              M Offline
              Mirko1980
              wrote on last edited by
              #6

              Let me guess: in the OnButtonClicked code, you are raising the ButtonClicked event? If yes, doing so casue the OnButtonClicked code to be recursively called non-stop, quickly filling the avaliable stack memory. In your OnButtonClicked, you have to put logic to handle the event, not to raise it.

              1 Reply Last reply
              0
              • S salon

                Hi all, I want to raise an event with delegates my code is like this: delcaration:

                public delegate void ButtonClickedDelegate(string path);
                public event ButtonClickedDelegate ButtonClicked;

                call:

                GetData(path);

                raise:

                private void GetData(string path)
                {
                try
                {
                if (ButtonClicked!= null)
                ButtonClicked(path);
                }
                catch
                {
                }
                }

                But I am getting ButtonClicked = null at the line

                if (ButtonClicked!= null)

                so that

                ButtonClicked(path);

                is not getting executed.... Do anybody have idea what is the wrong with the code?

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                ButtonClicked will be null if there are no subscribers to the event (therefore the delegate's InvocationList is empty). This example shows you how to do it. The ButtonClicked event here is initiated by the public PerformButtonClicked method being called on a class instance and in turn calling the OnButtonClicked method, but obviously in the real world the OnButtonClicked method would be called by the instance itself on response to a MouseDown or something.

                using System.Windows.Forms;

                namespace WindowsFormsApplication1
                {
                public partial class Form1 : Form
                {
                TestClass test;
                public Form1()
                {
                InitializeComponent();
                test = new TestClass();
                test.ButtonClicked += new TestClass.ButtonClickedDelegate(test_ButtonClicked);
                test.PerformButtonClicked(@"C:\Windows");
                }

                    void test\_ButtonClicked(string path)
                    {
                        MessageBox.Show(path);
                    }
                }
                
                public class TestClass
                {
                    public delegate void ButtonClickedDelegate(string path);
                    public event ButtonClickedDelegate ButtonClicked;
                
                    protected virtual void OnButtonClicked(string path)
                    {
                        ButtonClickedDelegate handler = ButtonClicked;
                        if (handler != null)
                            handler(path);
                    }
                
                    public void PerformButtonClicked(string path)
                    {
                        OnButtonClicked(path);
                    }
                }
                

                }

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                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