473,480 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is ByVal, ByRef, ParamArray in Declaration

14 New Member
I have seen some sample code with declaration statement using ByVal, ByRef and sometimes ParamArray.

Could anybody enlighten me the difference of these 3 and if possible with the short sample. I've read the Access Help, but it doesn't help me much to understand so I can use it.

Yus2Aces
Sep 14 '12 #1
10 4115
syedshaffee
91 New Member
ByVal is used as a parametre value in which the value of a variable will change at run time and Byref is also used as a param value but the value will not change and paramarray Specifies that a procedure parameter takes an optional array of elements of the specified type. ParamArray can be used only on the last parameter of a parameter list.
Sep 14 '12 #2
jack1005
1 New Member
Byval and Byref used as parameter for a procedure or functions.

Setting the parameter as byval it will pass data to a procedure/function. The value will not change after the call of the function.

example :
Expand|Select|Wrap|Line Numbers
  1. public sub check(byval sValue as string)
  2.   msgbox sValue
  3.   sValue="elo"
  4. End Sub
Setting the parameter as byref will pass data and return data to function/procedure and outside it.

example:
Expand|Select|Wrap|Line Numbers
  1. public sub check(byref sValue as string)
  2.   msgbox sValue
  3.   sValue="nice"
  4. end sub
--------the value of "sValue" will change after the procedure call.
Sep 14 '12 #3
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
The difference lies in whether you pass a copy of the variable/object, or you pass a link to the object. Sometimes its used to modify the variable or return more information then a simple function will allow, as a function returns just one value.

Imagine the 2 following subs:
Expand|Select|Wrap|Line Numbers
  1. Public Sub DemoByVal(ByVal strInput as string)
  2.   strInput="OverRide"
  3. End Sub
  4. Public Sub DemoByRef(ByRef strInput as string)
  5.   strInput="OverRide"
  6. End Sub
Now if we were to call these functions by the following code:
Expand|Select|Wrap|Line Numbers
  1. Public Sub DisplayDemo()
  2.   Dim strTest as string
  3.   strTest="Start"
  4.  
  5.   call DemobyVal(strTest)
  6.   Debug.print "DemoByVal: " & strTest
  7.  
  8.   Call DemoByRef(strTest)
  9.   Debug.print "DemoByRef: " & strTest
  10. End Sub
In your immediate pane it would then read:
Expand|Select|Wrap|Line Numbers
  1. DemoByVal: Start
  2. DemoByRef: OverRide
The default behavior is ByVal, so you don't actively put the ByRef then it will use ByVal.

In short you can say you should use ByVal (or nothing) when you want VBA to use the variables and do something or return something else, and you should use ByRef when you want VBA to work ON the variables, changing them for example.

Another example could be to pass error codes back and forth. Imagine a function call:
Expand|Select|Wrap|Line Numbers
  1. Public Function Example(ByRef ErrCode as long) as String
  2. On Error Goto ErrHandler
  3.   ...Some code that does something
  4.  
  5. ExitSub:
  6.   Exit Sub
  7.  
  8. ErrHandler:
  9.   errCode=Err.number 'Or custom error code
  10.   Resume ExitSub
  11. End Function

And you could use it in such a way:
Expand|Select|Wrap|Line Numbers
  1. Dim strTest as string
  2. Dim ErrCode as long
  3.  
  4. strTest=Example(ErrCode)
  5. If ErrCode<>0 then
  6.   Msgbox "Error: " & errCode" & vbnewline _
  7.          "Could not generate magic string"
  8.   GoTo ExitSub
  9. End if
In this way if a error occurs in the subprocedure, the calling main procedure becomes aware of it.
Sep 14 '12 #4
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
ParamArray is something you use for functions where you don't know how many arguments you want to issue. A good example could be a function to find a minimum. It might be that you want to find the minimum of 2 values, or it might be the minimum of 8 values. ParamArray is used to transmit a variable amount of arguments.
Sep 14 '12 #5
yus2aces
14 New Member
Thanks to all for the valuable answers. I'm still do not understand about ParamArray. Could anyone give an example about the practice?
Sep 14 '12 #6
yus2aces
14 New Member
Thank you so much again. Just read that TheSmileyCoder has give the best example. But others also already give me good explanation though. :D
Sep 14 '12 #7
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Glad to hear this worked for you.

Please do not consider my "extra" reply as any indication that there is something wrong with the first two answers, which there is not, I simply wanted to expand a bit on their explanation with more details.
Sep 14 '12 #8
yus2aces
14 New Member
Thank you TheSmileyCoder... you really has give me more clear understanding about this topic. :)
Sep 14 '12 #9
zmbd
5,501 Recognized Expert Moderator Expert
Very nice example by TheSmileyCoder

May I provide the simplest answer to this question that my prof gave us in class some 20 years ago:
(well, I thought it was the best in that it used real world objects... it's the chemist in me :-) )

You have a house on the street and it has a colour:
ByVal = the house is blue
ByRef = the house is located at 666 Mockingbird Ln and it is currently blue in colour.

So if you need to just know the colour of the house ByVal.
If you need to paint the house Red if the house is currently blue then ByRef.

-z
Sep 14 '12 #10
yus2aces
14 New Member
You give me clearer explanation by giving sample from real world objects. Thank you so much Zmbd. :)
Sep 15 '12 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

8
383
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...
4
5467
by: aaj | last post by:
Hi all can anyone shed a little light on to my Newbie problem? I have written my own collection that impliements the IEnumerator/Enumerable interfaces and on the surface, all seems well. ...
2
2406
by: hsharsha | last post by:
Consider the below code: int main(void) { class inner {}; friend class inner; /* what does this signify???? */ return 0; }
1
1583
by: toduro | last post by:
What is the right syntax for a C++ declaration which would give me __ls__7ostreamPFR7ostream_R7ostream in an object file produced by the gcc 2.95 compiler? The name "magically" appeared as...
2
1678
by: toduro | last post by:
Sorry about the bad subject line earlier. What is the right syntax for a C++ declaration which would give me __ls__7ostreamPFR7ostream_R7ostream in an object file produced by the gcc 2.95...
3
2645
by: samdomville | last post by:
hello...upon compilation, i get a warning: "warning: implicit declaration of funciton funciton_name" what, techicallly, is an implicit declaration? how/where would i look to track down the error?...
0
7049
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
6912
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
7052
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
7092
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...
0
6981
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.