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

ByVal call

Hello all!

I have a problem - some kind of theoretical problem.
What is the right call:
public function function_name (ByVal name1 as string) as string
or
public function function_name (name1 as string) as string

When should we use ByVal statement? Is it needed in the specifying a
function or procedure or when we have to write ByVal?

Thank you in advance for help
M.
Feb 8 '06 #1
1 2303
The "right call" is entirely dependant upon what the intent of the function
is. When ByVal is used, only the value of the variable, not the variable
itself is passed into the function. The default behavior is ByRef. When
ByRef (or nothing) is specified a reference to the argument variables is
passed so if the variable is changed in the called procedure, it will also
be changed in the calling procedure.

Example

Sub SomeProcedure ()
numValue = 7
debug.print numValue
debug.print MyFunction numValue
debug.print numValue
End Sub

Function MyFunction (A as integer)
A = 2 * A
MyFunction = 2 * A
End Function

Above, when SomeProcedure is run, it calls MyFunction. Since the argument
is passed ByRef (the default) and is changed by the function, the change
would be seen in SomeProcedure. Before the function is called, the value is
7, after it is 14.

Basically if there is any possibility that a passed argument variable might
get changed in a procedure and the change is not desired in the calling
procedure then ByVal should be used.

Hope this helps.

BTW - personally, if I were creating the BASIC language - I would make ByVal
the default. Surprisingly, they never asked me.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.
"news.onet.pl" <ma**************@poczta.onet.pl> wrote in message
news:ds**********@news.onet.pl...
Hello all!

I have a problem - some kind of theoretical problem.
What is the right call:
public function function_name (ByVal name1 as string) as string
or
public function function_name (name1 as string) as string

When should we use ByVal statement? Is it needed in the specifying a
function or procedure or when we have to write ByVal?

Thank you in advance for help
M.


Feb 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
3
by: WG | last post by:
Here is an ASP code sample in VBScript: <% Dim x Set x = server.CreateObject("ADODB.RECORDSET") x.CursorType = 1 x.CursorLocation = 3 x.Fields.Append "custtype", adVarNumeric, 80,...
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...
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,...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
7
by: Monty | last post by:
Silly question: If I return an object from a property, is it returned ByRef or ByVal? Is there a way to specify one way or the other? For instance, will the code that calls the MyObject() property...
5
by: Rob Meade | last post by:
Hi all, Until just recently most of my functions would have all been using Byval, when I realised the advantages of ByRef, where appropriate I have used it instead, my question - are there any...
9
Frinavale
by: Frinavale | last post by:
Hi there! For the last few days I've been running my code through the code analyzer that comes with Visual Studio 2005. I've found a whole bunch of useful design recommendations produced by this...
3
by: JB | last post by:
Hi All, I've discovered a strange behaviour with Object parameters passed ByVal via remoting and I'm wondering if anybody could shed some light on this. In a non remoting function call, when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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
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
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...

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.