How to reference an object created in another class?

  • Thread starter Thread starter Martin
  • Start date Start date
M

Martin

Hi Group

I apologize for this very basic (I guess) question. I had a look in
the posts and on MSDN but don't know where to start. I'm grateful for
any push in the right direction. I just don't know what I need to do.
Thank you so much!

Let's assume I've a procedure in class A which does some database
manipulation. When I run the procedure I want to open a SQL connection
and run some code. Since I have more than one procedure which opens a
connection, I don't want to write the code for opening a connection
all over again and I thought I put it in a separate class file B.

The class file B looks something like this:

Public Class clsData

Public Sub OpenSQLConection()
Dim SQLConnection As New System.Data.SqlClient.SqlConnection
SQLConnection.ConnectionString = "workstation
id=THEINTREPIDFOX;packet size=4096;user id=sa;data
source=""(local)"";p" & _
"ersist security info=False;initial catalog=Test"

SQLConnection.Open()

End Sub

Public Sub CloseSQLConnection()

Dim SQLConnection As System.Data.SqlClient.SqlConnection
SQLConnection.Close()

End Sub
End Class
-------------------------------------------------------------------------------
The code for class A from which I call the open database procedure is:

Imports TestApplication.clsData
Public Class clsValidate

Public Sub DataManipulation()

'Open database connection
clsData.OpenSQLConnection()

...Run Code

'Close database connection
clsData.CloseSQLConnection
End Sub

End Class

-------------------------------------------------------------------------------

How can I get the instance of the open connection so I can tell the
application under '...Run Code' that it shall use the connection being
opened in clsData?
How do I tell the CloseSQLConnection() procedure to close this very
open connection??

Apologies again for this simple question. I bet there are much more
errors in the code above. Please don't be too hard on me. :-)
 
The best and the simpliest solution is just to create an instance of your
connection class, open connection, run your data access, close connection,
dispose the instance. In your case (sorry, I don't use Basic):

Public Class clsValidate

Public Sub DataManipulation()

'Create in instance of clsData, say myConn
'Open database connection
myConn.OpenSQLConnection()

...Run Code

'Close database connection
myConn.CloseSQLConnection
'Dispose myConn

End Sub

End Class

It's a good idea to dispose all data connection objects explicitely,
otherwise you may run into the problem of growing number of connections.

Eliyahu
 
Make the "SQLConnection" object a private member of Class "clsData"
instead of a local member in the "OpenSQLConection" method.

For example,

Public Class clsData

Private m_SQLConnection As System.Data.SqlClient.SqlConnection

Public Sub OpenSQLConection()
m_SQLConnection = New System.Data.SqlClient.SqlConnection
SQLConnection.ConnectionString =
"workstation
id=THEINTREPIDFOX;packet size=4096;user
id=sa;data
source=""(local)"";p" & _
"ersist security info=False;initial
catalog=Test"

m_SQLConnection.Open()

End Sub

Public Sub CloseSQLConnection()

m_SQLConnection.Close()

End Sub
End Class

'*** In Class A, create an instance of clsData, and call the
OpenSQLConnection
'*** and CloseSQLConnection

Imports TestApplication
Public Class clsValidate

Public Sub DataManipulation()
Dim myClsData As New clsData

'Open database connection
myClsData.OpenSQLConnection()

...Run Code

'Close database connection
myClsData.CloseSQLConnection
End Sub

End Class

Tommy,

****************************************************************************
 
Back
Top