By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

Byval vs. byref

P: n/a
Forgive my newbie question, but I guess I don't understand passing
parameters, in my case an array.

I have a procedure which calls another procedure, passing an array. The new
procedure creates the array, but when it returns to the original procedure,
the array is lost, along with all the parameters I want modified by the
called procedure. I thought I understood the default was byref, which would
modify the actual variable, not just a copy. I haven't had this problem
before. How should I do this?

Dim arrClients as ArrayList
Private Sub DoStuff()
'do other stuff
Call CreateClientArray(arrClients, i, v, q)
End Sub

Public Class OtherStuff
Public Sub CreateClientArray(arrClients, i, v, q)
arrClients.Add("Client1")
arrClients.Add("Client2")
i = 1
v = "stuff"
q = "quarter"
end sub
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
The default way that arguments are passed in VB.NET is ByVal not ByRef.
"Iams" <Iams@hotmail> wrote in message
news:O4**************@TK2MSFTNGP14.phx.gbl...
Forgive my newbie question, but I guess I don't understand passing
parameters, in my case an array.

I have a procedure which calls another procedure, passing an array. The
new
procedure creates the array, but when it returns to the original
procedure,
the array is lost, along with all the parameters I want modified by the
called procedure. I thought I understood the default was byref, which
would
modify the actual variable, not just a copy. I haven't had this problem
before. How should I do this?

Dim arrClients as ArrayList
Private Sub DoStuff()
'do other stuff
Call CreateClientArray(arrClients, i, v, q)
End Sub

Public Class OtherStuff
Public Sub CreateClientArray(arrClients, i, v, q)
arrClients.Add("Client1")
arrClients.Add("Client2")
i = 1
v = "stuff"
q = "quarter"
end sub

Nov 19 '05 #2

P: n/a
"Iams" <Iams@hotmail> wrote in message news:O4**************@TK2MSFTNGP14.phx.gbl...
I thought I understood the default was byref, which would modify the
actual variable, not just a copy.
Your understanding of the difference between ByVal and ByRef is correct;
except that the default is ByVal.

: : Public Sub CreateClientArray(arrClients, i, v, q) Public Sub CreateClientArray( ByRef arrClients, i, v, q)

Here arrClients is passed-by-reference, whereas i, v and q
are passed-by-value.

Changes to arrClients in the Sub, CreateClientArray, affects
the variable arrClients in DoStuff( ) [it would even affect that
variable if it had a different name].

If you have any other questions on the subject, this MSDN Library
article explains everything,

http://msdn.microsoft.com/library/en...sMechanism.asp
Forgive my newbie question, but I guess I don't understand passing
parameters, in my case an array.


By the way, just a gentle nudge that VB.NET questions are best raised
in the microsoft.public.dotnet.languages.vb newsgroup. I've pointed
follow-up to that forum because it's probably a better place to discuss
this question than a newsgroup on ASP.NET (WebForms applications).
Derek Harmon
Nov 19 '05 #3

P: n/a
Hello
Dear Iams

in Vb.NET parameters are by default passed by Value not by Reference,
you should Write a function with parameters like
Public Sub abc(ByRef a as long)
' ' here you parameter a is available by reference .

end Sub
Kind Regards
Malik Asif

"Iams" <Iams@hotmail> wrote in message
news:O4**************@TK2MSFTNGP14.phx.gbl...
Forgive my newbie question, but I guess I don't understand passing
parameters, in my case an array.

I have a procedure which calls another procedure, passing an array. The new procedure creates the array, but when it returns to the original procedure, the array is lost, along with all the parameters I want modified by the
called procedure. I thought I understood the default was byref, which would modify the actual variable, not just a copy. I haven't had this problem
before. How should I do this?

Dim arrClients as ArrayList
Private Sub DoStuff()
'do other stuff
Call CreateClientArray(arrClients, i, v, q)
End Sub

Public Class OtherStuff
Public Sub CreateClientArray(arrClients, i, v, q)
arrClients.Add("Client1")
arrClients.Add("Client2")
i = 1
v = "stuff"
q = "quarter"
end sub

Nov 19 '05 #4

P: n/a
And, just to make it interesting...

If you pass a reference type (a class for example) ByVal (the default), you
WON'T get a copy of that object, you will get a copy of the pointer to that
object.
"Scott M." <s-***@nospam.nospam> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
The default way that arguments are passed in VB.NET is ByVal not ByRef.
"Iams" <Iams@hotmail> wrote in message
news:O4**************@TK2MSFTNGP14.phx.gbl...
Forgive my newbie question, but I guess I don't understand passing
parameters, in my case an array.

I have a procedure which calls another procedure, passing an array. The
new
procedure creates the array, but when it returns to the original
procedure,
the array is lost, along with all the parameters I want modified by the
called procedure. I thought I understood the default was byref, which
would
modify the actual variable, not just a copy. I haven't had this problem
before. How should I do this?

Dim arrClients as ArrayList
Private Sub DoStuff()
'do other stuff
Call CreateClientArray(arrClients, i, v, q)
End Sub

Public Class OtherStuff
Public Sub CreateClientArray(arrClients, i, v, q)
arrClients.Add("Client1")
arrClients.Add("Client2")
i = 1
v = "stuff"
q = "quarter"
end sub


Nov 19 '05 #5

P: n/a
Thanks all, that explains it! And thanks for the pointer info.
"Iams" <Iams@hotmail> wrote in message
news:O4**************@TK2MSFTNGP14.phx.gbl...
Forgive my newbie question, but I guess I don't understand passing
parameters, in my case an array.

I have a procedure which calls another procedure, passing an array. The new procedure creates the array, but when it returns to the original procedure, the array is lost, along with all the parameters I want modified by the
called procedure. I thought I understood the default was byref, which would modify the actual variable, not just a copy. I haven't had this problem
before. How should I do this?

Dim arrClients as ArrayList
Private Sub DoStuff()
'do other stuff
Call CreateClientArray(arrClients, i, v, q)
End Sub

Public Class OtherStuff
Public Sub CreateClientArray(arrClients, i, v, q)
arrClients.Add("Client1")
arrClients.Add("Client2")
i = 1
v = "stuff"
q = "quarter"
end sub

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.