Sorry for the newbie question. I'm passing around a database connection
into several forms. Do I use byref or byval for the variable, and why?
To me I should use byref, since I don't want a copy of the connection....

thanks
Chris

Public Sub New(ByVal MySqlConn As MySqlConnection)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()
Conn = MySqlConn
'Add any initialization after the InitializeComponent() call

End Sub

Re: ByVal vs. ByRef by Jon

Jon
Fri Jul 22 14:58:33 CDT 2005

Chris <no@spam.com> wrote:
> Sorry for the newbie question. I'm passing around a database connection
> into several forms. Do I use byref or byval for the variable, and why?
> To me I should use byref, since I don't want a copy of the connection....

See http://www.pobox.com/~skeet/csharp/parameters.html

It's about C#, but I believe all the same principles apply to VB.NET.
In short, use ByVal - you're not creating copies of the *object*, just
new references which "point" to the object.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: ByVal vs. ByRef by Marina

Marina
Fri Jul 22 15:04:28 CDT 2005

It would never be a copy of the actual connection. ByVal would mean it is a
copy of the *reference* to that connection. In either case there is only
ever 1 connection object.

The ByVal 'copy' can only happen with value types (integers, booleans, etc).

"Chris" <no@spam.com> wrote in message
news:ep4qRLvjFHA.1148@TK2MSFTNGP12.phx.gbl...
> Sorry for the newbie question. I'm passing around a database connection
> into several forms. Do I use byref or byval for the variable, and why? To
> me I should use byref, since I don't want a copy of the connection....
>
> thanks
> Chris
>
> Public Sub New(ByVal MySqlConn As MySqlConnection)
> MyBase.New()
>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
> Conn = MySqlConn
> 'Add any initialization after the InitializeComponent() call
>
> End Sub



Re: ByVal vs. ByRef by Chris

Chris
Fri Jul 22 15:11:11 CDT 2005

Marina wrote:
> It would never be a copy of the actual connection. ByVal would mean it is a
> copy of the *reference* to that connection. In either case there is only
> ever 1 connection object.
>
> The ByVal 'copy' can only happen with value types (integers, booleans, etc).
>
> "Chris" <no@spam.com> wrote in message
> news:ep4qRLvjFHA.1148@TK2MSFTNGP12.phx.gbl...
>
>>Sorry for the newbie question. I'm passing around a database connection
>>into several forms. Do I use byref or byval for the variable, and why? To
>>me I should use byref, since I don't want a copy of the connection....
>>
>>thanks
>>Chris
>>
>> Public Sub New(ByVal MySqlConn As MySqlConnection)
>> MyBase.New()
>>
>> 'This call is required by the Windows Form Designer.
>> InitializeComponent()
>> Conn = MySqlConn
>> 'Add any initialization after the InitializeComponent() call
>>
>> End Sub
>
>
>

Ahh, copy the reference not the object, that makes sense now....

Chris

Re: ByVal vs. ByRef by Scott

Scott
Fri Jul 22 15:14:49 CDT 2005

Reference Type passed ByVal (the most common scenario) = Copy of the
reference, not the object
Reference Type passed ByRef = Pointer to the original reference
Value Type passed ByVal = Copy of the value type
Value Type passed ByRef = Pointer to the original value type


"Chris" <no@spam.com> wrote in message
news:ep4qRLvjFHA.1148@TK2MSFTNGP12.phx.gbl...
> Sorry for the newbie question. I'm passing around a database connection
> into several forms. Do I use byref or byval for the variable, and why? To
> me I should use byref, since I don't want a copy of the connection....
>
> thanks
> Chris
>
> Public Sub New(ByVal MySqlConn As MySqlConnection)
> MyBase.New()
>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
> Conn = MySqlConn
> 'Add any initialization after the InitializeComponent() call
>
> End Sub