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

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 3.00 out of 5)
 

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")

        Else

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

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

        End If

Leave a Reply