JoJo
JoJo

Reputation: 4933

What does ByRef mean in vbscript when passing to a procedure?

I have the following start to a procedure call

strReturn = OrderCreate(strGen8, _
                        strUID, _
                        Order, _
                        boolRecapFlag, _

And on the function that receives the parameters we have..

function OrderCreate(strCoDiv, strUID, byRef Order, boolRecap, strBillFirst, etc.

Since I could not find anywhere where Order values were getting passed into the prodcedure. Am I to assume that the ByRef makes it possible to bring data out of the procedure? Using the Order variable name?

Upvotes: 1

Views: 7504

Answers (3)

Average Joe
Average Joe

Reputation: 4601

The difference between byref and byval

dim my_org_age,my_wife_org_age

my_org_age = 43
my_wife_org_age = 43

Call make_our_ages_younger(my_org_age, my_wife_org_age)

After the above sub ( notice it's not even a function! ), my_org_age and my_wife_org_age wil be ten years younger!

That's because the corresponding function here takes its args byREf

sub make_our_ages_younger(byref my_age,byref her_age)
    my_age = my_Age-10
    her_age = her_age-10
end sub

Now, if you remove the "byRef" words out of it, and then run

Call make_our_ages_younger(my_org_age, my_wife_org_age)

you will notice that our ages won't get changed.

See the difference what byRef does now?

HTH

Upvotes: 1

Nilpo
Nilpo

Reputation: 4816

Parameters can be passed into a function in two ways: by reference (ByRef) or by value (ByVal). In VBScript, the default method is ByRef.

When you pass a value by reference, you are passing a reference that variables address in memory. This means that any changes made within your function will persist once your function exists. It can also be used to help control the way your script manages memory since data stored in a variable is only written once in memory.

Passing a parameter by value creates a copy of the variable in memory within the new scope. Changes made to this information within the new scope have no affect on the data in other scopes.

Upvotes: 4

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

byRef means you are passing a reference to the original variable. So if you change the value in the function it is reflected back on the original

This differs from byVal which passes the values and makes an independent copy.

Upvotes: 1

Related Questions