Results 1 to 8 of 8

Thread: [RESOLVED] Raising an event when the object is instantiated

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Raising an event when the object is instantiated

    Quote Originally Posted by Coding4Fun View Post
    What am I doing wrong?
    Um, that would be this:
    Quote Originally Posted by Coding4Fun View Post
    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    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

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Raising an event when the object is instantiated

    Quote Originally Posted by PlausiblyDamp View Post
    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.

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Raising an event when the object is instantiated

    Quote Originally Posted by jmcilhinney View Post
    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    Re: Raising an event when the object is instantiated

    Quote Originally Posted by PlausiblyDamp View Post
    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Raising an event when the object is instantiated

    Quote Originally Posted by Coding4Fun View Post
    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
  •  



Click Here to Expand Forum to Full Width