Results 1 to 13 of 13

Thread: display Event data from a class

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    display Event data from a class

    Ok I have a class that I want to handle it's own events but still have the instance inherit it.

    for instance I want the event to display in a text box but from class level.

    Make sense?

    Here's my code:

    vb.net Code:
    1. Private Sub InitializeHandlers()
    2.         AddHandler Me.OnConnectionEvent, AddressOf OnConnection
    3.         'AddHandler Me.OnAuthentication, AddressOf OnAuthenticated
    4.     End Sub
    5.  
    6.     Public Function OnConnection(ByVal sender As Object, ByVal e As ConnectionEventArgs)
    7.  
    8.         Return e.ConnectionData & vbCrLf
    9.  
    10.     End Function
    11.  
    12.     Public Sub Connect()
    13.         clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    14.         Dim endpoint As IPEndPoint = New IPEndPoint(Dns.GetHostEntry(_remoteHostIP).AddressList(0), _remotePort)
    15.  
    16.         Try
    17.  
    18.             clientSocket.Connect(endpoint)
    19.             If clientSocket.Connected = True Then
    20.                 RaiseEvent OnConnectionEvent(Me, New ConnectionEventArgs(True))
    21.             End If
    22.         Catch ex As SocketException
    23.             RaiseEvent OnConnectionEvent(Me, New ConnectionEventArgs(False, ex.Message))
    24.             'Console.Write(ex.Message)
    25.         End Try
    26.  
    27.     End Sub
    28.  
    29. Public Class ConnectionEventArgs
    30.     Inherits TcpEventArgs
    31.  
    32. #Region "Decelartions"
    33.  
    34.     Private _connected As Boolean
    35.     Private _data As String
    36.  
    37. #End Region
    38.  
    39. #Region "Constructors"
    40.  
    41.     Public Sub New(ByVal connected As Boolean)
    42.         MyBase.New(-1, "")
    43.         _connected = connected
    44.     End Sub
    45.  
    46.     Public Sub New(ByVal connected As Boolean, ByVal data As String)
    47.         MyBase.New(-1, "")
    48.         _connected = connected
    49.         _data = data
    50.     End Sub
    51.  
    52. #End Region
    53.  
    54. #Region "Properties"
    55.  
    56.     Public ReadOnly Property Connected() As Boolean
    57.         Get
    58.             Return _connected
    59.         End Get
    60.     End Property
    61.  
    62.     Public ReadOnly Property ConnectionData() As String
    63.         Get
    64.             Return _data
    65.         End Get
    66.     End Property
    67.  
    68. #End Region
    69.  
    70. End Class

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: display Event data from a class

    No, what you do doesn't make any sense due to the following reasons:
    1. There is no valid reason to have a class to listen to one of its own event. If the class raises an event then it already know exactly when that event is raised so it can do what ever needed right then. Why does it have to listen for the event in order to do something?

    2. Your event handler is a function... I never seen this before, but this is not the point I'm after. The point is that a function will return a value, and to get that returned value, you have to catch it when the function is called. For example:
    Code:
    Dim x as Integer = GetMeAnInteger()
    
    Private Function GetMeAnInteger() As Integer
        Dim rand as new random
        Return rand.Next(Integer.MinValue, Integer.MaxValue)
    End Function
    So if you just call the function like this, even though there is some value being returned, it's not being assigned to anything, so the returned value will just be floating in memory until collected by GC. And this is the case with your event handler. When your class raise the event, the event handler function execute but the returned value is not being used by any thing.
    Code:
    GetMeAnInteger()
    3. You should turn both Option Explicit and Option Strict ON.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: display Event data from a class

    they are on now. Option strict was off... also this is a class, so the Instance created at runtime from the users code will have to call the function and yes save it to a variable.

    However I am new to events and was curious how I could side cut all the adding handlers via the programmers side of the code and interpret into my class. Then return the value.

    But that wont work.

    I suppose I could make it so that the programmers code can create a procedure and use the handles clause to handle the instance.OnConnection event.
    Last edited by TheUsed; Nov 6th, 2009 at 12:36 PM.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: display Event data from a class

    How are EventArgs normally handled? Class side as well as programmers code side?

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: display Event data from a class

    created in the class... passed as part of the event signature... where the event handler catches it...

    -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??? *

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: display Event data from a class

    So is this following code, standard?

    vb.net Code:
    1. '''********************Class:******************************
    2. Public Class Test
    3.  
    4. Private Sub InitializeHandlers()
    5.         AddHandler Me.OnConnectionEvent, AddressOf OnConnection
    6.         'AddHandler Me.OnAuthentication, AddressOf OnAuthenticated
    7.     End Sub
    8.  
    9.  Public Sub Connect()
    10.         clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    11.         Dim endpoint As IPEndPoint = New IPEndPoint(Dns.GetHostEntry(_remoteHostIP).AddressList(0), _remotePort)
    12.  
    13.         Try
    14.  
    15.             clientSocket.Connect(endpoint)
    16.  
    17.             If clientSocket.Connected = True Then
    18.  
    19.                 RaiseEvent OnConnectionEvent(Me, New ConnectionEventArgs(True))
    20.                 getResponse()
    21.  
    22.             End If
    23.  
    24.         Catch ex As SocketException
    25.  
    26.             RaiseEvent OnConnectionEvent(Me, New ConnectionEventArgs(False, ex.Message))
    27.             'Console.Write(ex.Message)
    28.         End Try
    29.  
    30. End Sub
    31.  
    32. End Class
    33. Public Class ConnectionEventArgs
    34.     Inherits TcpEventArgs
    35.  
    36. #Region "Decelartions"
    37.  
    38.     Private _connected As Boolean
    39.     Private _data As String
    40.  
    41. #End Region
    42.  
    43. #Region "Constructors"
    44.  
    45.     Public Sub New(ByVal connected As Boolean)
    46.         MyBase.New(-1, "")
    47.         _connected = connected
    48.     End Sub
    49.  
    50.     Public Sub New(ByVal connected As Boolean, ByVal data As String)
    51.         MyBase.New(-1, "")
    52.         _connected = connected
    53.         _data = data
    54.     End Sub
    55.  
    56. #End Region
    57.  
    58. #Region "Properties"
    59.  
    60.     Public ReadOnly Property Connected() As Boolean
    61.         Get
    62.             Return _connected
    63.         End Get
    64.     End Property
    65.  
    66.     Public ReadOnly Property ConnectionData() As String
    67.         Get
    68.             Return _data
    69.         End Get
    70.     End Property
    71.  
    72. #End Region
    73.  
    74. End Class
    75.  
    76. '''*************************Program:**********************************
    77.  Public Sub OnConnection(ByVal sender As Object, ByVal e As ConnectionEventArgs)
    78.  
    79.         Console.WriteLine("Connected: " & e.Connected.ToString)
    80.  
    81.     End Sub

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: display Event data from a class

    Suppose your class expose an event named SomethingHappenedEvent. The sole purpose of this event is to let the world (outside of the class) knows that something has happened. So when your class raises this event, any other classes that listen for the event will get notified so they can do whatever needed.
    The standard way of declaring an event in .net is
    Code:
    Public SomethingHappenedEvent(ByVal sender As Object, ByVal e As SomethingHappenedEventArgs)
    The "sender" is always the object that raises that event, and the "e" is an instance of the event args that contains the important data about the event that your class wants to pass to the listeners. If this is a custom event, you normally create your own event args class that inherits form the System.EventArgs class.
    The class doesn't handle its own events since there is no need to. Other classes who listen to the event will handle the event.
    So in other words, events are a way of communication between classes. By raising an event, a class is saying to the world that something has happened with it. So anyone in that world who is interesting in this "news" can know when it happened and perform some action accordingly.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: display Event data from a class

    Having a class handle an event that it raised is most definitely not standard. Nor is it efficient. I'm still not clear on what you are trying to do, but a class never needs to respond to its own events, so you can remove the event entirely unless code outside the class will be handling it.

    Also not sure what you mean by "Class side" vs. programmer code side, but it looks like design time vs runtime.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: display Event data from a class

    Thank you stanav, I needed to inherit the system.eventargs.

    This allowed me to use the dropdown boxes in the IDE to automatically create the procedures for my events.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: display Event data from a class

    I just typed out a nice post on what I was actually rtying to accomplish and then my IE foobared upon sending the reply

    Here we go again.

    correct Shaggy_Hiker I did mean design time vs runtime. I couldn't find the words.

    I guess I was trying to get it so that the programmer could use my class and override a default action performed by my classes event. For instance like when you can override a controls OnPaint event.

    Stanav was able to inform me about the system.eventargs namespace and after adding that it appears that I can now click the dropdown box inside the code window and click my declared object of my class that uses withevents and then I can click on my events class.

    this will then create a procedure that handles my event. Before I had to hardcode the addhandler and create the sub on my own. So this sidesteps some coding.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: display Event data from a class

    If you set the class up right, then a different programmer can inherit from your class, and override any of the methods they want. If you want to do something like this, one of the things you will have to recognize is the difference between Private and Protected. Private is only visible to members of the class. Protected is like Private, except that Protected members are visible to the class, and any class derived from it. Therefore, you would want all Private members to be Protected, instead.
    My usual boring signature: Nothing

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: display Event data from a class

    If you want to learn about defining and raising your own events, I just added a post to my blog on that subject recently. Follow the link in my signature.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: display Event data from a class

    Thank you, I will read your blog.

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