473,549 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to reference an object created in another class?

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 OpenSQLConectio n()
Dim SQLConnection As New System.Data.Sql Client.SqlConne ction
SQLConnection.C onnectionString = "workstatio n
id=THEINTREPIDF OX;packet size=4096;user id=sa;data
source=""(local )"";p" & _
"ersist security info=False;init ial catalog=Test"

SQLConnection.O pen()

End Sub

Public Sub CloseSQLConnect ion()

Dim SQLConnection As System.Data.Sql Client.SqlConne ction
SQLConnection.C lose()

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 DataManipulatio n()

'Open database connection
clsData.OpenSQL Connection()

...Run Code

'Close database connection
clsData.CloseSQ LConnection
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 CloseSQLConnect ion() 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. :-)
Nov 18 '05 #1
3 1564
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 DataManipulatio n()

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

...Run Code

'Close database connection
myConn.CloseSQL Connection
'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

"Martin" <th************ @hotmail.com> wrote in message
news:72******** *************** ***@posting.goo gle.com...
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 OpenSQLConectio n()
Dim SQLConnection As New System.Data.Sql Client.SqlConne ction
SQLConnection.C onnectionString = "workstatio n
id=THEINTREPIDF OX;packet size=4096;user id=sa;data
source=""(local )"";p" & _
"ersist security info=False;init ial catalog=Test"

SQLConnection.O pen()

End Sub

Public Sub CloseSQLConnect ion()

Dim SQLConnection As System.Data.Sql Client.SqlConne ction
SQLConnection.C lose()

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 DataManipulatio n()

'Open database connection
clsData.OpenSQL Connection()

...Run Code

'Close database connection
clsData.CloseSQ LConnection
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 CloseSQLConnect ion() 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. :-)

Nov 18 '05 #2
Make the "SQLConnect ion" object a private member of Class "clsData"
instead of a local member in the "OpenSQLConecti on" method.

For example,

Public Class clsData

Private m_SQLConnection As System.Data.Sql Client.SqlConne ction

Public Sub OpenSQLConectio n()
m_SQLConnection = New System.Data.Sql Client.SqlConne ction
SQLConnection.C onnectionString =
"workstatio n
id=THEINTREPIDF OX;packet size=4096;user
id=sa;data
source=""(local )"";p" & _
"ersist security info=False;init ial
catalog=Test"

m_SQLConnection .Open()

End Sub

Public Sub CloseSQLConnect ion()

m_SQLConnection .Close()

End Sub
End Class

'*** In Class A, create an instance of clsData, and call the
OpenSQLConnecti on
'*** and CloseSQLConnect ion

Imports TestApplication
Public Class clsValidate

Public Sub DataManipulatio n()
Dim myClsData As New clsData

'Open database connection
myClsData.OpenS QLConnection()

...Run Code

'Close database connection
myClsData.Close SQLConnection
End Sub

End Class

Tommy,

*************** *************** *************** *************** *************** *

th************@ hotmail.com (Martin) wrote in message news:<72******* *************** ****@posting.go ogle.com>...
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 OpenSQLConectio n()
Dim SQLConnection As New System.Data.Sql Client.SqlConne ction
SQLConnection.C onnectionString = "workstatio n
id=THEINTREPIDF OX;packet size=4096;user id=sa;data
source=""(local )"";p" & _
"ersist security info=False;init ial catalog=Test"

SQLConnection.O pen()

End Sub

Public Sub CloseSQLConnect ion()

Dim SQLConnection As System.Data.Sql Client.SqlConne ction
SQLConnection.C lose()

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 DataManipulatio n()

'Open database connection
clsData.OpenSQL Connection()

...Run Code

'Close database connection
clsData.CloseSQ LConnection
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 CloseSQLConnect ion() 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. :-)

Nov 18 '05 #3
Thank you very much for your help!
You've made my day!

We**********@Ho tmail.com (Tommy) wrote in message news:<a8******* *************** ****@posting.go ogle.com>...
Make the "SQLConnect ion" object a private member of Class "clsData"
instead of a local member in the "OpenSQLConecti on" method.

For example,

Public Class clsData

Private m_SQLConnection As System.Data.Sql Client.SqlConne ction

Public Sub OpenSQLConectio n()
m_SQLConnection = New System.Data.Sql Client.SqlConne ction
SQLConnection.C onnectionString =
"workstatio n
id=THEINTREPIDF OX;packet size=4096;user
id=sa;data
source=""(local )"";p" & _
"ersist security info=False;init ial
catalog=Test"

m_SQLConnection .Open()

End Sub

Public Sub CloseSQLConnect ion()

m_SQLConnection .Close()

End Sub
End Class

'*** In Class A, create an instance of clsData, and call the
OpenSQLConnecti on
'*** and CloseSQLConnect ion

Imports TestApplication
Public Class clsValidate

Public Sub DataManipulatio n()
Dim myClsData As New clsData

'Open database connection
myClsData.OpenS QLConnection()

...Run Code

'Close database connection
myClsData.Close SQLConnection
End Sub

End Class

Tommy,

*************** *************** *************** *************** *************** *

th************@ hotmail.com (Martin) wrote in message news:<72******* *************** ****@posting.go ogle.com>...
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 OpenSQLConectio n()
Dim SQLConnection As New System.Data.Sql Client.SqlConne ction
SQLConnection.C onnectionString = "workstatio n
id=THEINTREPIDF OX;packet size=4096;user id=sa;data
source=""(local )"";p" & _
"ersist security info=False;init ial catalog=Test"

SQLConnection.O pen()

End Sub

Public Sub CloseSQLConnect ion()

Dim SQLConnection As System.Data.Sql Client.SqlConne ction
SQLConnection.C lose()

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 DataManipulatio n()

'Open database connection
clsData.OpenSQL Connection()

...Run Code

'Close database connection
clsData.CloseSQ LConnection
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 CloseSQLConnect ion() 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. :-)

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1918
by: Nick Zdunic | last post by:
I have a remotable object running in my host application. The host starts up and creates the object. Within a method to start the remote object doing its thing it creates an object. This object reference is passed into another object create within the same
17
1614
by: johny smith | last post by:
I never really know if I should use references or pointers for constructors. Can anyone give me some guidance on when to use what? My back ground is C, so I tend to use pointer notation, but really don't know what the advantage of the reference it. Thanks alot. ex. of two constructors
2
2159
by: Jim Red | last post by:
hello first of all, i know, there are no classes in javascript. but i will use that word for better understanding of my question. here we go. i have three classes and need a reference to the parent class. could this be done by a reference, or just by constructing the class. function Class1() { this.class2 = new Class2();
1
1241
by: Arne Claus | last post by:
Hello I've written a usefull little smartpointer, implementing reference counitng. I'm using this class for quite a while but have new enocuntered a nasty little problem: After I've created a reference counted Object like this CRefPtr<A> ptr(new A()); I want to use something implement like this at a completely differend
4
3395
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any...
7
1561
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As Integer = 0 To LinkResults.Length - 1 With MyCurrentClass.SqlCmd_Insert_LinkVB .CommandType = System.Data.CommandType.StoredProcedure
14
7161
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your opinion? Vol
5
2061
by: Tom | last post by:
If I have a container class that has a map member which stores pointers to objects that have been created via the new operator and I have a method that returns a entry in the map, would it be best to return the entry as a dereferenced pointer so it becomes a class or return a reference to the dereferenced class? I believe that it would be...
7
5767
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
7
2811
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of...
0
7527
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7967
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1953
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.