-
Jun 10th, 2021, 06:25 AM
#1
Thread Starter
New Member
[RESOLVED] Raising an event when the object is instantiated
Hello friends
I'm trying to fire up an event when my object is instantiated right after the form show's event, but it's not working.
Here is what I did:
Code:
Public Class MyObject
Public Event FileNotFoundEvent(message As String)
Public Sub New()
If Not System.IO.File.Exists("bla") Then
RaiseEvent FileNotFoundEvent("File not found.")
End If
End Sub
End Class
My form's code..
Code:
Private WithEvents TestClass As MyObject
Public Sub OnFileNotFoundEvent(message As String) Handles TestClass.FileNotFoundEvent
MessageBox.Show(message)
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
TestClass = New MyObject()
End Sub
What am I doing wrong?
-
Jun 10th, 2021, 06:50 AM
#2
Re: Raising an event when the object is instantiated
Originally Posted by Coding4Fun
What am I doing wrong?
Um, that would be this:
Originally Posted by Coding4Fun
I'm trying to fire up an event when my object is instantiated
That whole concept is wrong. You can't register an event handler unless you have a reference to the object raising the event and you can't possibly have a reference to an object if it hasn't been constructed yet.
-
Jun 10th, 2021, 07:42 AM
#3
Thread Starter
New Member
Re: Raising an event when the object is instantiated
This is how I solved, not the best way, but works.
Code:
Public Class MyObject
Public Event FileNotFoundEvent(message As String)
Public Sub New()
End Sub
Public ReadOnly Property FileExists As Boolean
Get
If Not System.IO.File.Exists("bla") Then
RaiseEvent FileNotFoundEvent("File not found.")
Return False
Else
Return True
End If
End Get
End Property
End Class
My form...
Code:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
TestClass = New MyObject()
Dim a = TestClass.FileExists
End Sub
-
Jun 10th, 2021, 09:35 AM
#4
Re: Raising an event when the object is instantiated
That seems an utterly confusing way of doing things! Why have a property that returns true or false and also raise an event? It seems a pointless duplication of effort.
It might be easier if you explained what you are trying to achieve rather than what your proposed solution is in this case.
If you are trying to create an instance of an object and the file is required then you could just throw an exception if the file isn't present, that way the object doesn't get created if the file is missing and the calling code would get a Catchable exception so it could deal with the problem.
-
Jun 10th, 2021, 09:52 AM
#5
Re: Raising an event when the object is instantiated
Originally Posted by PlausiblyDamp
If you are trying to create an instance of an object and the file is required then you could just throw an exception if the file isn't present, that way the object doesn't get created if the file is missing and the calling code would get a Catchable exception so it could deal with the problem.
Throwing exceptions in constructors is a no-no. You can do it but it's bad form.
-
Jun 10th, 2021, 11:08 AM
#6
Re: Raising an event when the object is instantiated
Originally Posted by jmcilhinney
Throwing exceptions in constructors is a no-no. You can do it but it's bad form.
I was working on the assumption of "less bad" rather than anything else
-
Jun 10th, 2021, 03:41 PM
#7
Thread Starter
New Member
Re: Raising an event when the object is instantiated
Originally Posted by PlausiblyDamp
I was working on the assumption of "less bad" rather than anything else
Yes, I was trying to solve it by raise event just because I like this way
but it's completely unnecessary. A simple Message Box would be enough, and the Exception solution seems to be the most proper way.
-
Jun 10th, 2021, 07:39 PM
#8
Re: Raising an event when the object is instantiated
Originally Posted by Coding4Fun
the Exception solution seems to be the most proper way.
Not if it's being thrown in the constructor, it doesn't. Throwing an exception when trying to access the file would be OK. If you want to check for the file in the constructor then setting a read-only property that indicates whether the object is valid or invalid would be the most proper way.
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
|