473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ByRef parameters for objects / reference types

Hi....

Assume Function A in an application calls Function GetSomeData in another
assembly..... which then is the prefered method to return the SqlDatareader
object back to Function A (and why ?).

Does the prefered option apply to all reference types ?

Option 1
*******
Public Function GetSomeData (ByRef dr as SqlDataReader) as Long

Option 2
*******
Public Function GetSomeData (ByVal dr as SqlDataReader) as Long

Option 3
*******
Public Function GetSomeData () as SqlDataReader
Is it better to pass "objects" ByVal or ByRef (from a performance
perspective) ?
Cheers!
Nov 20 '05 #1
3 1732
which then is the prefered method to return the SqlDatareader
object back to Function A (and why ?).
Option 1 and 3 are the only two that let you return a new
SqlDataReader object. Option 2 only lets you modify an existing one.

Whether you should use 1 or 3 depends on if you're actually using the
Long return value in #1 for something useful or not.

Is it better to pass "objects" ByVal or ByRef (from a performance
perspective) ?


From a performance point of view I don't think it makes any
difference. Do whatever makes sense for the semantics of your method.
ByRef indicates that the parameter can be reassigned to refer to a new
object.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #2
Tinman,
Is it better to pass "objects" ByVal or ByRef (from a performance
perspective) ? Do not code for performance first, code for "correctness" first. Code only
for performance when a specific routine via profiling has proven to have a
performance problem!

The "correct" thing to do is to pass all parameters ByVal unless you
explicitly need to by ByRef, you only need to pass ByRef when you are
changing the callers variable itself (not the contents of the object the
variable refers to, but the variable itself!).

Which appears is what you are really asking how to do, that is return an
object from GetSomeData. I would use Option 3 as its more obvious (when you
call it) that you are returning a DataReader. Option 2 will not return a
data reader (it accepts a data reader as input).

Option 1 can be used to return the object, however it has "side effects"
(you are returning a value from the function, plus returning a value in a
parameter). I rarely code Functions with ByRef parameters as its not obvious
what they are doing ("side effects" are not very "correct"). I would either
code a Sub with 2 ByRef parameters if I need two values back, or a function
that returned an object/structure with the two values. Depending on what the
second value is (an status perhaps) I would return it via more appropriate
means (an exception instead of a status).

Hope this helps
Jay

"tinman" <dw****@yahoo.com> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl... Hi....

Assume Function A in an application calls Function GetSomeData in another
assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why ?).

Does the prefered option apply to all reference types ?

Option 1
*******
Public Function GetSomeData (ByRef dr as SqlDataReader) as Long

Option 2
*******
Public Function GetSomeData (ByVal dr as SqlDataReader) as Long

Option 3
*******
Public Function GetSomeData () as SqlDataReader
Is it better to pass "objects" ByVal or ByRef (from a performance
perspective) ?
Cheers!

Nov 20 '05 #3
Hi Tinman,

When it is about a SqlDatareader I would very good read the description of
the Class in MSDN.
In other cases I follow Jay.

http://msdn.microsoft.com/library/de...ClassTopic.asp

Cor
Nov 20 '05 #4

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

Similar topics

8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
6
by: Cc | last post by:
hi, is there a way to use byref on property set , because i would like to pass the value into the variable byref ?
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
19
by: Rob Panosh | last post by:
Hello, Ok here is the senerio: ..... Dim myArrayList as New ArrayList(0) me.Test_A( myArrayList )
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
7
by: barrett bonden | last post by:
Is there any way to pass parameters to a function and simply know there will get there without the silly (C like ) complexity of worring about byval and or perhaps byref ? (Why bother to...
9
by: Samuel Shulman | last post by:
Hi I wander there is a way to return ByRef just like passing ByRef What I want to achieve is the following: Call a method that returns an object Use that call as an argument to a ByRef...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.