Declaring variables

Cassio

Junior Contributor
Joined
Nov 30, 2002
Messages
276
Location
Rio de Janeiro
Hi, I´d like to know if theres any diference between these two aproaches:

1.
Visual Basic:
Private cn as SQLConnection

Private Sub Sub1()

    cn = New SQLConnection(connectionstring)
    
    'Code here
    
     cn.close

End Sub

Private Sub Sub2()

   cn = New SQLConnection(connectionstring)
    
   'Code here
    
    cn.close

End Sub


2.
Visual Basic:
Private Sub Sub1()
    Dim cn as SQLConnection

    cn = New SQLConnection(connectionstring)
    
    'Code here
    
     cn.close

End Sub

Private Sub Sub2()
   Dim cn as SQLConnection

   cn = New SQLConnection(connectionstring)
    
   'Code here
    
    cn.close

End Sub

I guess theres no diference, since I instanciate the cn object inside the procedures, right?

Thanks.
 
There is a difference, even though you initialize the instance inside of a sub/function that variable itself is global to the class and when initializing you are crating an instance for that variable.
 
I would say yes since the variable is there as long the class is running.

While in the second it is only declared when the sub is called and then disposed when it is done
 
Back
Top