Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am I right in assuming that:
(for example): If we have a variable called "DB" as a oledb.oledbconnection

and I wanted to pass that connection to another function in a module in my project that the receiving sub/function has

testfunction(byval DB as oledb.oledbconnection)
means that "testfunction" creates a new connection while

testfunction(byref DB as oledb.oledbconnection)
would use the existing connection opened elsewhere in my project?

Thanks!

What I have tried:

curious in understand how a connection can be passed around in an application so i minimise the amount of connections i am making to a database
Posted
Updated 17-Nov-16 1:56am

 
Share this answer
 
Comments
Member 12561559 17-Nov-16 7:45am    
I have read that (thank you for the link btw), but it doesn't really say about a database connection. Whether it is re-created (I think it is if its BYVAL) or if it is re-used (BYREF) - I am only asking because I have recently moved from VB6 to VB.NET and am still trying to grasp how things work and just wanted a confirmation on whether I have understood that correctly? Thanks!
Dave Kreskowiak 17-Nov-16 8:54am    
A DbConnection object is a reference type. All class instances (objects) are reference types. Value types are things like Integers and structures.

No, nothing is ever re-created. For reference types, you're always passing a reference to the object. The difference being is if you pass ByVal, you're passing in a COPY of the reference (not a copy of the object). If you try to replace the object passed in with a different object, nothing happens as far as the caller is concerned. When your method returns, the caller will still have the original object it passed in.

In a ByRef situation, you're passing in the original reference to the object. In this case, if the method called replaced the object with a different one, the caller would get that modified object back, essentially replacing the one that it passed in.
Member 12561559 17-Nov-16 10:10am    
Thanks! The reason I am asking is this: In vb6 I write a DLL, deploy it on IIS6 (yes we seriously need to update) and everything runs fine there are no conflicts i.e. everything running in a DLL for a user connected on a session is "their" DLL, all variables belong to that particular user, but since using VB.NET Im getting an idea (from what I am reading, though I may be misunderstanding) that if I have a DLL and is has some public variables declared, that even though there are multiple users, that one user that has accessed our DLL, may be getting the values of that public variable that another user, on a different session but still accessing the DLL, is getting - does that make sense? Sorry if I sound confusing. Basically because of the multiple threading of VB.NET, that anything declared as Public, is available to all that access that variable. Im going to write a simple test to see if that is indeed true. Its mindboggling moving from VB to .NET as far as .COM aware .NET DLLS (which is what I am having to do).
Richard MacCutchan 17-Nov-16 10:38am    
Sorry, I don't know the answer. When you say 'public' variables, where and how are they made available? A DLL should not hold information that belongs to a client application.
Member 12561559 17-Nov-16 10:57am    
I think I need to do a lot more reading, the differences on how .NET handles DLLs and VB handling DLLs is very different, even though the set up is very similar. I seem to be getting threading issues on database connections, yet the same code in VB runs fine without the same issues. More reading me thinks - anyway, thank you very much Richard - I have a lot of things to read and think about. Your time has been most appreciated :)
The passed argument is a "reference class". That means even when using ByVal, a reference is passed but you get a copy of the reference instead of the reference itself (you will not get a copy of the OleDBConnection instance).

So the answer to your question is:
The existing connection is used in both cases.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900