Tuesday, October 16, 2012

How create new object instances

How create new object instances

The new function – how to create object with Try Catch


Variables must first be declared and then only after created using this.

        Dim oClass1 As Class1 'Initiate variable
        Try
            oClass1 = New Class1 'create object

        Catch ex As Exception
            'add basic error message here
            MsgBox(ex.StackTrace, MsgBoxStyle.Exclamation, ex.Message)
        End Try


Declare all your variables above the Try. This will never crash and is not a mistake. If there was error, it would come from an earlier time.

The function new may cause an error for many reasons. When inside the Try-Catch, it is possible to trap the error and also correct it.

The example above is too simple, but imagine for a moment that line would cause an error. Automatically, the program intercepts the bug and moves to catch thus avoiding continue the rest of the code in the Try.
And since this article was to talk about the new function, I can not miss error handling or good programming practices.
I will also have to force myself to set my brain of crap.

About

I invite you to visit my blog for more articles and leave a comment.
Check Technologies represents more than 10 years .... Computer and computer aided design.







No comments:

Post a Comment