Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi, I´d like to know if theres any diference between these two aproaches:

 

1.

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.

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.

  • *Experts*
Posted
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.
Posted

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

Dream as if you'll live forever, live as if you'll die today

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...