Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 39,374 times

Contents

Related Categories

Passing Variables By Value or Reference - Odd Effects of Passing Arguments

mrjdesign

Odd Effects of Passing Arguments

Now how would this possibly affect our passing values between functions. Assume for a moment that we try to pass a value ByRef between two statements. We pass the memory allocation address to the next statement, and the statement will then attempt to access the data in its memory location.

If the data type is not of the same type as the one requested by the called statement, you would receive an error at compile time already. This is very good to know, as this means you will minimize the risk of sending a bug out to the end users.

However, sending the same variable to the same statement, using ByVal has a different result. Assume you are sending the Integer value 1024 to a statement requiring a ByVal MyLong As Long. This would result in a copy of the value 1024 being sent to the statement.

It would also approve of the value it self, as it fits within a Long.

Integers hold 2 bytes of memory and range between -32,768 to 32,767 while Long or also referred to as "long integers" hold 4 bytes of memory space and range between -2,147,483,648 to 2,147,483,647.

So in other words, somewhere in the depth of the language, there is a check from the compiler to see that the correct Variable Type is being passed internally as well.

One way to avoid this data type verification is to pass a ByRef value as an expression instead of assigning a variable type to the value. Visual Basic evaluates the expression and will attempt to pass the result of the expression if it can.

The simplest way to pass a variable as an expression is simply to enclose it in parenthesis.

For example Call MyFunction(AnInteger)

Function MyFunction(ByRef TheString As String)
MsgBox "The string is " & TheString
End Function


©2001 - All Rights Reserved - MRJ Design
The source code samples and information pertained within this document are considered copyrighted material and may not be re-distributed by electronic or other media in any form or fashion whatsoever, withou

Comments

  • Thanks :)

    Posted by mrjdesign on 05 Feb 2003

    It was fun to write it too... I still havn't gotten the hang on all ins and outs yet but I'm learning as I help others.

  • not that much

    Posted by http://vbxzone.tripo on 20 Nov 2001

    it was not a bad article. However it was kind of too abstract, and no code examples or anything. just pure bs on byref and byval

  • Informative

    Posted by outvit on 28 Oct 2001

    This was a very informative article. It was a pleasure to read.