Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing an open OleDbConnection?

Status
Not open for further replies.

RichieMac

Programmer
Aug 18, 2004
61
0
0
GB
Hi

Is it possible to pass an open connection to another class?
 
Hey Richie,

I know that you can pass a connection between functions, so I don't see why you couldn't pass it to a class, as long as the constructor took it as a parameter. Though if you're going to be using it and want to close it in the class, you may need to pass it by reference.

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Though if you're going to be using it and want to close it in the class, you may need to pass it by reference
As far as I know, You don't need to pass the connection by reference in order to close it. You need to pass a reference type instance by reference only when you write something like that:
Code:
public void fun(MyClass MyInstance)
{
   MyInstance = New MyInstance();
}
Or:
Code:
public void fun(MyClass MyInstance)
{
   MyInstance = null;
}
You don't need to pass by reference if you write:
Code:
public void fun(MyClass MyInstance)
{
   MyInstance.MyInt = 3;
}
Or in you case:
Code:
public void fun(OleDbConnection MyCon)
{
   MyCon.Close();
}
 
A connection object is an object like any other, so it obeys all the variable passing rules just as if it were a string, or an int, etc.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
A connection object is an object like any other, so it obeys all the variable passing rules just as if it were a string, or an int, etc.
Are yu sure about that? int is a value type and a connection object is a reference type, so if you pass an int to a method, it can change only the copy of this int. String is a reference type, but since it is immutable, it behaves like an int in this case.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top