xmlnewbie
asked on
Raising Events and Throwing Exceptions
I'm creating a custom class and I need some of my methods to raise events or throw custom exceptions. How would I go about doing this? What are the best practices for this? I want everything self-contained in my custom class. Is this possible or advisable?
ASKER
1. So, I can declare these events in the class that I am making?
2. Where is myeventhandler supposed to be placed?
3. Can I really expect people using my class to add "AddHandler y.myevent, AddressOf myeventhandler" to their code just to catch my events? Can't it be raised and handled similar to how, say, the Button1.Click event is raised and handled?
4. What about custom exceptions? Do I need to create a totally different class for that??? That would be odd to me since I also don't think I can expect people using my class to make sure they have my custom exception class.
2. Where is myeventhandler supposed to be placed?
3. Can I really expect people using my class to add "AddHandler y.myevent, AddressOf myeventhandler" to their code just to catch my events? Can't it be raised and handled similar to how, say, the Button1.Click event is raised and handled?
4. What about custom exceptions? Do I need to create a totally different class for that??? That would be odd to me since I also don't think I can expect people using my class to make sure they have my custom exception class.
ASKER
Ronald?
Sorry xmlnewbie, Completly forgot about this question :-),
I haven got much time today (I have to work too :-)) but look at this site it explains very well how to build controls (exposing events, properties etc)
http://www.dotnet247.com/247reference/a.aspx?u=http://www.gotdotnet.com/quickstart/winforms/doc/WinFormsCreatingControls.aspx
I haven got much time today (I have to work too :-)) but look at this site it explains very well how to build controls (exposing events, properties etc)
http://www.dotnet247.com/247reference/a.aspx?u=http://www.gotdotnet.com/quickstart/winforms/doc/WinFormsCreatingControls.aspx
ASKER
Thanks. I'll look at the link. My concerns are stated in a previous post. Also, my question deals with throwing custom exceptions. Any guidance on that?
ASKER
Ronald, Any guidance on throwing exceptions? I'm still studying the link on raising events. Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ohhhhhhhhhhhh. Now, throwing exceptions makes sense to me. I can make the exception class part of the custom class that I am developing. Meaning, I don't need to have my class users use another separate class just to catch my exceptions.
Still going thru your link.
Still going thru your link.
ASKER
What are you supposed to do with Inner in:
Public Sub New(ByVal Message As String, ByVal Inner As Exception)
Public Sub New(ByVal Message As String, ByVal Inner As Exception)
Public Class mytest
Public Event myevent(ByVal Text As String)
Public Sub myrun()
For x As Integer = 1 To 10
'whatever
Next
RaiseEvent myevent("finished")
End Sub
End Class
Private Sub myeventhandler(ByVal s As String)
MsgBox(s)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim y As New mytest
AddHandler y.myevent, AddressOf myeventhandler
y.myrun()
End Sub