Click to See Complete Forum and Search --> : just a verification on parameter passing
JKeenan
April 23rd, 2006, 12:46 PM
sub Foo1()
dim zArray() Double
redim zArray(1000)
Foo2(zArray)
Foo3(zArray)
End Foo1
sub Foo2( byref zArray() Double)
do something with zArray
End Foo2
sub Foo3( byval zArray() Double)
do something with zArray
End Foo2
calling Foo2 from Foo1 is the best way to pass arrays (byref)
there is no boxing going on?
calling Foo3, creates a copy of the array on the stack and passes
the contents/value in
Still don't know if there is any boxing going on in this one.
Craig Gemmill
April 23rd, 2006, 03:42 PM
When you pass value types (int, double, etc.) byval, a copy of the object is created on the stack for local use. Exactly like the Foo3() definition shows.
Boxing is not byval/byref sensitive, it's actually a totally seperate "feature" in OOP. Boxing occurs when you conceal a value type inside of a reference type. Unboxing, of course, is when you unwrap the object to expose the value type. Take the following function for example:
Sub Foo4(myValue as Object)
End Sub
Notice that we've declared myValue as an Object. Now if you pass a value type as a parameter, it will box that variable inside of an Object.
Foo4(1)
Foo4(2)
Foo4(4)
Foo4(8)
If you pass a reference type as the parameter, it will just become a pointer to the object being passed, and no boxing will occur.
JKeenan
April 23rd, 2006, 05:03 PM
what are arrays passed as , as in the examples above?
Not worried about scalar values.
To my understanding (from what I read)... Boxing occurs when a situation occurs like passing in a value but the parameter signature wants a reference.... then boxing occurs... takes the value... ccopies it and passes the address of the copy into the function....
Craig Gemmill
April 23rd, 2006, 06:18 PM
Boxing only occurs when you try to use a VALUE TYPE as a REFERENCE TYPE (usually Object).
Dim obj as Object
Dim foo as Int32 = 1248
obj = foo '<- Boxing occurs here. It wraps the value type in an Object.
Boxing does not happen when passing parameters, unless you have an REFERENCE TYPE as a parameter and you pass it a VALUE TYPE.
Reference Type - ByVal
The pointer to the object is passed as is.
Reference Type - ByRef
A pointer to the pointer to the object is passed.
Value Type - ByVal
The value is copied to a new local variable.
Value Type - ByRef
A pointer is passed to the existing variable.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.