[Delegates and Events]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johnny E Jensen

    [Delegates and Events]

    Hi
    Using C#, VS2005 .NET2

    I have a custom usercontrol.
    Here i have a picturebox (Close) and a CheckedListBox

    A public delegate void for CloseControlHan dler();, and event for that
    CloseControlEve nt;
    A public delegate void for AddFieldToListH andler(string tablename, object
    item); and event for that AddFieldToListE vent;

    When the user clicks the picture, the picturebox.clic k event fires. Here I
    raises my CloseControlEve nt, this allways works fine.
    When the user clicks on a item in the CheckListBox the
    CheckListBox.It emCheck is fired (besause the CheckOnClick property = true),
    I call a method to raise my event AddFieldToListE vent, but always i'll get
    the 'Object reference not set....' Error. it seems that the
    AddFieldToListE vent i allways NULL.
    I do use the:
    If ( AddFieldToListE vent != null ) to prevent the application to crash.

    Could anyone provide with some hints.

    The Code:
    public delegate void AddFieldToListH andler(string tablename,
    FieldDescriptio nClass description);
    public event AddFieldToListH andler AddFieldToListE vent;

    public delegate void CloseControlHan dler(Control ClosingControl) ;
    public event CloseControlHan dler CloseControlEve nt;

    private void picClose_Click( object sender, EventArgs e)
    {
    if ( CloseControlEve nt !=null)
    {
    CloseControlEve nt(this);
    }
    }

    private void lstFields_ItemC heck(object sender, ItemCheckEventA rgs e)
    {
    RaiseAddEvent(_ strTablename, _objField);
    }

    private void RaiseAddEvent(s tring tablename, FieldDescriptio nClass
    description)
    {
    if ( AddFieldToListE vent != null) // this AddFieldToListE vent is always
    null ?????????
    AddFieldToListE vent(tablename, description);
    }

    Kind Regards
    Johnny E Jensen


  • Jon Skeet [C# MVP]

    #2
    Re: [Delegates and Events]

    Johnny E Jensen wrote:

    <snip>
    Could anyone provide with some hints.
    It's not clear why you expect the event *not* to be null. Instead of
    providing partial code, could you provide a short but complete program
    which demonstrates the problem?

    See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
    that.

    In addition, if you're storing a handler separately to the event, you
    would normally implement the add/remove behaviour by hand to use that
    handler delegate. It's possible that I've misunderstood the point of
    the separate delegate/event due to only seeing part of the code though.

    See http://www.pobox.com/~skeet/csharp/events.html for more details of
    this.

    Jon

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: [Delegates and Events]

      Johnny E Jensen wrote:

      <snip>
      Could anyone provide with some hints.
      It's not clear why you expect the event *not* to be null. Instead of
      providing partial code, could you provide a short but complete program
      which demonstrates the problem?

      See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
      that.

      In addition, if you're storing a handler separately to the event, you
      would normally implement the add/remove behaviour by hand to use that
      handler delegate. It's possible that I've misunderstood the point of
      the separate delegate/event due to only seeing part of the code though.

      See http://www.pobox.com/~skeet/csharp/events.html for more details of
      this.

      Jon

      Comment

      • Dave Sexton

        #4
        Re: [Delegates and Events]

        Hi Johnny,

        You should consider following Microsoft's standards for event design:

        ".NET Framework Developer's Guide, Event Design"


        Also, in the 2.0 framework you no longer need to create custom delegates.
        Instead, you can use the generic EventHandler<TE ventArgsdelegat e if you
        need to use custom event arguments. Create your own class that derives from
        System.EventArg s and use it for the TEventArgs parameter.

        --
        Dave Sexton

        "Johnny E Jensen" <jej@winner-crm.dkwrote in message
        news:elMFBAGIHH A.960@TK2MSFTNG P04.phx.gbl...
        Hi
        Using C#, VS2005 .NET2
        >
        I have a custom usercontrol.
        Here i have a picturebox (Close) and a CheckedListBox
        >
        A public delegate void for CloseControlHan dler();, and event for that
        CloseControlEve nt;
        A public delegate void for AddFieldToListH andler(string tablename, object
        item); and event for that AddFieldToListE vent;
        >
        When the user clicks the picture, the picturebox.clic k event fires. Here I
        raises my CloseControlEve nt, this allways works fine.
        When the user clicks on a item in the CheckListBox the
        CheckListBox.It emCheck is fired (besause the CheckOnClick property =
        true), I call a method to raise my event AddFieldToListE vent, but always
        i'll get the 'Object reference not set....' Error. it seems that the
        AddFieldToListE vent i allways NULL.
        I do use the:
        If ( AddFieldToListE vent != null ) to prevent the application to crash.
        >
        Could anyone provide with some hints.
        >
        The Code:
        public delegate void AddFieldToListH andler(string tablename,
        FieldDescriptio nClass description);
        public event AddFieldToListH andler AddFieldToListE vent;
        >
        public delegate void CloseControlHan dler(Control ClosingControl) ;
        public event CloseControlHan dler CloseControlEve nt;
        >
        private void picClose_Click( object sender, EventArgs e)
        {
        if ( CloseControlEve nt !=null)
        {
        CloseControlEve nt(this);
        }
        }
        >
        private void lstFields_ItemC heck(object sender, ItemCheckEventA rgs e)
        {
        RaiseAddEvent(_ strTablename, _objField);
        }
        >
        private void RaiseAddEvent(s tring tablename, FieldDescriptio nClass
        description)
        {
        if ( AddFieldToListE vent != null) // this AddFieldToListE vent is always
        null ?????????
        AddFieldToListE vent(tablename, description);
        }
        >
        Kind Regards
        Johnny E Jensen
        >
        >

        Comment

        Working...