PC Review


Reply
Thread Tools Rate Thread

ByVal vs. ByRef

 
 
Chris
Guest
Posts: n/a
 
      22nd Jul 2005
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
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Jul 2005
Chris <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      22nd Jul 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      22nd Jul 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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
 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      22nd Jul 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ByVal and ByRef in VB.Net =?Utf-8?B?TWljaGVs?= Microsoft Dot NET 3 1st Mar 2005 08:23 PM
byVal Vs. byRef Rob Panosh Microsoft VB .NET 11 16th Dec 2003 12:53 AM
ByVal and ByRef Goncalo Microsoft ASP .NET 1 4th Dec 2003 09:57 AM
ByRef or ByVal Hei Microsoft VB .NET 7 18th Nov 2003 03:42 PM
Is ByVal always better if ByRef isn't necessary Jeff Microsoft Excel Programming 5 25th Jul 2003 09:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:47 PM.