|
-
Nov 6th, 2009, 11:13 AM
#1
Thread Starter
Fanatic Member
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:
Private Sub InitializeHandlers()
AddHandler Me.OnConnectionEvent, AddressOf OnConnection
'AddHandler Me.OnAuthentication, AddressOf OnAuthenticated
End Sub
Public Function OnConnection(ByVal sender As Object, ByVal e As ConnectionEventArgs)
Return e.ConnectionData & vbCrLf
End Function
Public Sub Connect()
clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim endpoint As IPEndPoint = New IPEndPoint(Dns.GetHostEntry(_remoteHostIP).AddressList(0), _remotePort)
Try
clientSocket.Connect(endpoint)
If clientSocket.Connected = True Then
RaiseEvent OnConnectionEvent(Me, New ConnectionEventArgs(True))
End If
Catch ex As SocketException
RaiseEvent OnConnectionEvent(Me, New ConnectionEventArgs(False, ex.Message))
'Console.Write(ex.Message)
End Try
End Sub
Public Class ConnectionEventArgs
Inherits TcpEventArgs
#Region "Decelartions"
Private _connected As Boolean
Private _data As String
#End Region
#Region "Constructors"
Public Sub New(ByVal connected As Boolean)
MyBase.New(-1, "")
_connected = connected
End Sub
Public Sub New(ByVal connected As Boolean, ByVal data As String)
MyBase.New(-1, "")
_connected = connected
_data = data
End Sub
#End Region
#Region "Properties"
Public ReadOnly Property Connected() As Boolean
Get
Return _connected
End Get
End Property
Public ReadOnly Property ConnectionData() As String
Get
Return _data
End Get
End Property
#End Region
End Class
-
Nov 6th, 2009, 12:20 PM
#2
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.
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 -
-
Nov 6th, 2009, 12:31 PM
#3
Thread Starter
Fanatic Member
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.
-
Nov 6th, 2009, 12:45 PM
#4
Thread Starter
Fanatic Member
Re: display Event data from a class
How are EventArgs normally handled? Class side as well as programmers code side?
-
Nov 6th, 2009, 12:50 PM
#5
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
-
Nov 6th, 2009, 12:56 PM
#6
Thread Starter
Fanatic Member
Re: display Event data from a class
So is this following code, standard?
vb.net Code:
'''********************Class:******************************
Public Class Test
Private Sub InitializeHandlers()
AddHandler Me.OnConnectionEvent, AddressOf OnConnection
'AddHandler Me.OnAuthentication, AddressOf OnAuthenticated
End Sub
Public Sub Connect()
clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim endpoint As IPEndPoint = New IPEndPoint(Dns.GetHostEntry(_remoteHostIP).AddressList(0), _remotePort)
Try
clientSocket.Connect(endpoint)
If clientSocket.Connected = True Then
RaiseEvent OnConnectionEvent(Me, New ConnectionEventArgs(True))
getResponse()
End If
Catch ex As SocketException
RaiseEvent OnConnectionEvent(Me, New ConnectionEventArgs(False, ex.Message))
'Console.Write(ex.Message)
End Try
End Sub
End Class
Public Class ConnectionEventArgs
Inherits TcpEventArgs
#Region "Decelartions"
Private _connected As Boolean
Private _data As String
#End Region
#Region "Constructors"
Public Sub New(ByVal connected As Boolean)
MyBase.New(-1, "")
_connected = connected
End Sub
Public Sub New(ByVal connected As Boolean, ByVal data As String)
MyBase.New(-1, "")
_connected = connected
_data = data
End Sub
#End Region
#Region "Properties"
Public ReadOnly Property Connected() As Boolean
Get
Return _connected
End Get
End Property
Public ReadOnly Property ConnectionData() As String
Get
Return _data
End Get
End Property
#End Region
End Class
'''*************************Program:**********************************
Public Sub OnConnection(ByVal sender As Object, ByVal e As ConnectionEventArgs)
Console.WriteLine("Connected: " & e.Connected.ToString)
End Sub
-
Nov 6th, 2009, 01:07 PM
#7
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 -
-
Nov 6th, 2009, 01:19 PM
#8
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
 
-
Nov 6th, 2009, 01:25 PM
#9
Thread Starter
Fanatic Member
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.
-
Nov 6th, 2009, 01:38 PM
#10
Thread Starter
Fanatic Member
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.
-
Nov 6th, 2009, 04:40 PM
#11
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
 
-
Nov 7th, 2009, 02:43 AM
#12
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.
-
Nov 10th, 2009, 08:22 AM
#13
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|