473,320 Members | 1,845 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,320 software developers and data experts.

Byval vs. byref

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
5 2531
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
"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
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
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
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 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...
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...
14
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
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...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
8
by: Boni | last post by:
Dear all, I found out that I don' understand byVal/byRef in VB. There is a simple example: Why in the first test the result is 10,10 where in the second 0,20. Thanks for your help. Boni Module...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.