Check if Class/Object needs new instance or if its disposed, ect

This works with All versions of VB.NET up to VB 2010.

        'Check before getting the 'Object reference not set to an instance of an object' error 
        'message is going to been thrown! The code below will check if the variable to the 
        'Class/Object has been set to a instance of the object, disposed, ect.. - WITHOUT getting a 
        'error in your apps.

        Dim audioObject As Microsoft.DirectX.AudioVideoPlayback.Audio

        If audioObject Is Nothing Then

            MessageBox.Show("This object is not usable yet! You need to set a new instance of the object!", _

                " Not usable!", MessageBoxButtons.OK, MessageBoxIcon.Error)

            'Now set a new instance of the object since you found out it was set!

            audioObject = New Microsoft.DirectX.AudioVideoPlayback.Audio("c:\windows.wav")


            MessageBox.Show("This object is usable!", _

                " It is Usable!", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.