Results 1 to 5 of 5

Thread: Should i create custom event

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2011
    Posts
    194

    Should i create custom event

    Sometime i need to create an event and pass just one value, should i create a class inherited from EventArgs or just pass my value

    Example

    Should i do

    vb Code:
    1. ' Declare calss MyEventArgs
    2.     Public Class MyEventArgs
    3.         Inherits EventArgs
    4.         Public Value As Integer
    5.  
    6.     End Class
    7.  
    8. ' Declare MyEvent
    9.     Public Event MyEvent(ByVal sender As Object, ByVal e As MyEventArgs)
    10.  
    11. ' Raise MyEvent
    12.     Dim e As New MyEventArgs
    13.     e.Value = 90
    14.    RaiseEvent MyEventArgs(Me, e)

    or just


    vb Code:
    1. ' Declare MyEvent
    2.     Public Event MyEvent(ByVal sender As Object, ByVal e As Integer)
    3.  
    4. ' Raise MyEvent
    5.    RaiseEvent MyEvent(Me, 90)
    On Error GoTo Hell

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Should i create custom event

    I prefer the first one. Looks more meaningful.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2011
    Posts
    194

    Re: Should i create custom event

    Quote Originally Posted by Toph View Post
    I prefer the first one. Looks more meaningful.
    But it require more typing without any benefit except following coding standards.
    On Error GoTo Hell

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,951

    Re: Should i create custom event

    if you're the only consumer of the event, then do it how ever you want... no one is going to care. But if you do that, you better be consistant and do it that same way EACH AND EVERYTIME, even when you're passing 5 values, even when you're passing 15 values. It doesn't take much to extend the EventArgs class to add one value - 4 lines:
    Code:
        Public Class MyEventArgs
            Inherits EventArgs
            Public Value As Integer
        End Class
    But hey, you knew that...

    But if others are going to consume it... you'll be better off following the traditional convention. EventArgs provides more information than just the value.

    Oh, you'll find out that there are a fair amount of events which only pass one value in the framework.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Should i create custom event

    With regards to 'more typing'; if typing is your biggest time consumer on a project, then there's something wrong.

    That aside, I agree with TG; by getting into the habit of inheriting eventArgs, following the convention, you'll satisfy a lot of consumers of that event.

    As an example, by passing back EventArgs, you can add meaningful information about the event. If you just pass back a number, what does it mean? Your event name has to carry a lot more of the load in describing what it does. In addition, you have the ability to pass information back to the method that raised the event (for example, many events have a cancel mechanism).

    There are a lot of reasons for inheriting eventargs, and none for not following the convention.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width