473,401 Members | 2,127 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,401 software developers and data experts.

byref and byvalue

ari
hey all,

i have the following 2 classes:
Public Class DataAccessLayer
....
....
Public Sub GetRecords(ByRef ds As DataSet1)
ds = New DataSet1
Me.SqlDataAdapter1.Fill(ds)
End Sub
End Class
Here's the class that uses the above class:

Public Class Class1
Public Function GetCustomers() As DataSet1
Dim ds As DataSet1
Dim dal As New DataAccessLayer
dal.GetRecords(ds)
Return ds
End Function
End Class

in the DataAccessLayer class, i thought using byref or byvalue doesn't make
a difference since the parameter is a reference variable. am i nuts?

thanks,
ari
Feb 13 '06 #1
6 1936
Lee
Ari:

When a reference variable is passed, either ByVal or ByRef, its
components can be modified. In your example, the properties of DataSet1
can be modified, no matter how 'ds' is passed.

But, when passed ByVal, 'ds' itself will not be changed. For that you
must pass 'ds'ByRef.

--
Lee Silver
Information Concepts Inc.

Feb 13 '06 #2

"ari" <ar*@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
:
: hey all,
:
: i have the following 2 classes:
:
:
: Public Class DataAccessLayer
: ...
: ...
: Public Sub GetRecords(ByRef ds As DataSet1)
: ds = New DataSet1
: Me.SqlDataAdapter1.Fill(ds)
: End Sub
: End Class
:
:
: Here's the class that uses the above class:
:
: Public Class Class1
: Public Function GetCustomers() As DataSet1
: Dim ds As DataSet1
: Dim dal As New DataAccessLayer
: dal.GetRecords(ds)
: Return ds
: End Function
: End Class
:
: in the DataAccessLayer class, i thought using byref or byvalue doesn't
: make a difference since the parameter is a reference variable. am i
: nuts?
:
: thanks,
: ari
ByRef means that any changes to the underlying variable you are passing into
the function will be visible when the function exists. ByVal in turn means
any such changes are discarded. The key is to understanding exactly what it
is you are passing in as a parameter.
In this case, you are passing in a reference value - that is, a pointer to
an object on the heap. If you pass it in ByRef, the specific instance of
the object type that the pointer is referencing can be changed when the
function exits. If you pass it in ByVal however, any such changes are
discarded. Either way, any changes to the instance of the object itself are
visible after the function exits.
Consider a simple example:
'--------------------------------------------------------
Public Class TestClass
Public n As Integer = 0
End Class
Public Module [module]
Private Sub SubOne(ByRef TCls As TestClass)
TCls = New TestClass
TCls.n = 10
End Sub

Private Sub SubTwo(ByVal TCls As TestClass)
TCls = New TestClass
TCls.n = 10
End Sub

Public Sub Main()
Dim TClsOne As TestClass
Dim TClsTwo As TestClass

TClsOne = New TestClass
TClsTwo = TClsOne
TClsOne.n = 1

Console.WriteLine("Original object values: ")
Console.WriteLine("TClsOne: " & TClsOne.n)
Console.WriteLine("TClsTwo: " & TClsTwo.n)
Console.WriteLine

SubOne(TClsOne)
SubTwo(TClsTwo)
Console.WriteLine("Object values after calling subs: ")
Console.WriteLine("TClsOne: " & TClsOne.n)
Console.WriteLine("TClsTwo: " & TClsTwo.n)

End Sub

End Module
'--------------------------------------------------------

Here we have a simple class with a single Public Integer member. Our module
in turn creates two references to the same object:
'--------------------------------------------------------
Dim TClsOne As TestClass
Dim TClsTwo As TestClass

TClsOne = New TestClass
TClsTwo = TClsOne
TClsOne.n = 1
'--------------------------------------------------------
While we've defined two object references, we actually only create a single
instance of the object. Both references variables TClsOne and TClsTwo in
fact point to the same instance of the TestClass object. A change to the
member variable 'n' by the first reference is therefore reflected in the 2nd
reference. When we output the first section we see the following:
'--------------------------------------------------------
Original object values:
TClsOne: 1
TClsTwo: 1
'--------------------------------------------------------
Note that the change to the value of TClsOne.n is seen in the reference to
TClsTwo.n - again these two reference variable are pointing to the same
object instance in memory.
Now let's consider what is happening when we call our two subs (which differ
only by the fact that one parameter is passed in ByRef and the other is
passed in ByVal):
'--------------------------------------------------------
Private Sub SubOne(ByRef TCls As TestClass)
TCls = New TestClass
TCls.n = 10
End Sub

Private Sub SubTwo(ByVal TCls As TestClass)
TCls = New TestClass
TCls.n = 10
End Sub

[...]
SubOne(TClsOne)
SubTwo(TClsTwo)
[...]
'--------------------------------------------------------
In both cases a brand new instance of the TestClass object is create on the
heap and the reference variable is changed to point to it. We then change
the value of 'n' for that new object and exit. However, when we print out
the second output section, we see a different result:

'--------------------------------------------------------
Object values after calling subs:
TClsOne: 10
TClsTwo: 1
'--------------------------------------------------------
Note that now the value of TClsOne.n is set to '10' whereas the value of
TClsTwo.n remains '1'. This is because in SubOne(), the TCls parameter is
passed in ByRef. Therefore, when we created the new TestClass object inside
the sub and set the TCls variable to point to it, that assignment remained
in place when the sub exited. At that point, TClsOne is pointing to the 2nd
instance of the TestClass object while TClsTwo is still pointing to the
original reference.
When we call SubTwo, the same steps occur inside the procedure. This time
however, the new reference is discarded when SubTwo exits and TClsTwo
therefore still points to the original object instance that it did prior to
calling the sub.
To summarize, the ByRef/ByVal keywords are relevant to both Value types and
Reference types. In the case of the Reference type, what you are passing in
is a pointer to an object (hence the term 'Reference' type). If you change
the instance that the parameter is pointing to, that reassignment is
retained when the Sub/Function exits if the parameter is passed in ByRef and
discarded if the parameter is passed in ByVal.
HTH.
Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.

Feb 13 '06 #3
Ari,

Did you know that this code gives the same effect

Public Class DataAccessLayer
...
...
Public Function GetDataSet1() as DataSet1 dim ds as New DataSet1
Return Me.SqlDataAdapter1.Fill(ds) End Sub
End Class
Here's the class that uses the above class:

Public Class Class1 Public Function GetCustomers() As DataSet1
Dim dal As New DataAccessLayer
dim ds as DataSet1 = dal.GetDataSet1()
End Function End Class

I find this much nicer, a call in a Sub in a seperated class confuse me
forever, than still have to see what happens there with that given dataset.

Keep in mind that you are only passing references.

Just my thought,

Cor
Feb 13 '06 #4
"ari" <ar*@discussions.microsoft.com> schrieb
hey all,

i have the following 2 classes:
Public Class DataAccessLayer
...
...
Public Sub GetRecords(ByRef ds As DataSet1)
ds = New DataSet1
Me.SqlDataAdapter1.Fill(ds)
End Sub
End Class
Here's the class that uses the above class:

Public Class Class1
Public Function GetCustomers() As DataSet1
Dim ds As DataSet1
Dim dal As New DataAccessLayer
dal.GetRecords(ds)
Return ds
End Function
End Class

in the DataAccessLayer class, i thought using byref or byvalue
doesn't make a difference since the parameter is a reference
variable. am i nuts?


http://groups.google.com/group/micro...e83beb9988aabf
Armin

Feb 13 '06 #5
CMM
With objects:

ByRef allows the subroutine to actually change the object that <ds> points
to. Remember variables are NOT "objects"... they are references to objects
(that exist somewhere in la la land).

ByVal does not allow the subroutine to change what <ds> points to. If it
tries to changes the reference (ds = New Dataset), the runtime steps in and
breaks the reference to the original <ds> so that the calling subroutine is
not affected.

--
-C. Moya
www.cmoya.com
"ari" <ar*@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
hey all,

i have the following 2 classes:
Public Class DataAccessLayer
...
...
Public Sub GetRecords(ByRef ds As DataSet1)
ds = New DataSet1
Me.SqlDataAdapter1.Fill(ds)
End Sub
End Class
Here's the class that uses the above class:

Public Class Class1
Public Function GetCustomers() As DataSet1
Dim ds As DataSet1
Dim dal As New DataAccessLayer
dal.GetRecords(ds)
Return ds
End Function
End Class

in the DataAccessLayer class, i thought using byref or byvalue doesn't
make
a difference since the parameter is a reference variable. am i nuts?

thanks,
ari

Feb 13 '06 #6
ari
wow, thanks for the awesome feedback. you guys are great.

"ari" wrote:
hey all,

i have the following 2 classes:
Public Class DataAccessLayer
...
...
Public Sub GetRecords(ByRef ds As DataSet1)
ds = New DataSet1
Me.SqlDataAdapter1.Fill(ds)
End Sub
End Class
Here's the class that uses the above class:

Public Class Class1
Public Function GetCustomers() As DataSet1
Dim ds As DataSet1
Dim dal As New DataAccessLayer
dal.GetRecords(ds)
Return ds
End Function
End Class

in the DataAccessLayer class, i thought using byref or byvalue doesn't make
a difference since the parameter is a reference variable. am i nuts?

thanks,
ari

Feb 14 '06 #7

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...
10
by: Logico | last post by:
Hi everybody, I've tried to use the byref keyword for passing arguments to subroutines and functions in my ASP pages with VBScript, but it seems that both byref and byval are irrilevant, as simple...
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 )
5
by: Frank | last post by:
Hi, in the code below, does it matter, in this case, whether I use byval or byref? I don't think so but I would like confirmation. In both case I don't change the actual value of pdgts, just the...
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,...
4
by: pamela fluente | last post by:
I look for an opinion. I am wirting a huge number of Rectangles on a graphic object. I have a lot of places where these rectangles are passed as parameters of some function. Provided that...
9
by: ManicQin | last post by:
Hello all. I've templated a simple class like the next: template <class _T> class base { public: base(_T newVal):m_data(newVal){}
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.