Byval vs. byref

I

Iams

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
 
D

Derek Harmon

Iams said:
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-us/vbcn7/html/vaconArgPassMechanism.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
 
M

Malik Asif Joyia

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
 
S

Scott M.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top