By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,806 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

Why can't I use BYREF in my Set Sub of a property.

P: n/a
Hi.

I have a class and it exposes a property, which accepts a parameter
collection object.
I want the class to use the parameter object and update it.
However I don't want to use a copy of the collection.
So I innocently open up my class, and modify the Set sub to have BYRef
instead of BYVal, and the IDE says 'Set' parameter cannot be declared as
'ByRef'.
Why?
How can I offer my class an object (any object) with out it taking a copy to
work with (byval).

Thanks

Alex
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Alex Stevens" <al**@matrixinfotech.co.uk_nospam> schrieb
Hi.

I have a class and it exposes a property, which accepts a
parameter collection object.
I want the class to use the parameter object and update it.
However I don't want to use a copy of the collection.
So I innocently open up my class, and modify the Set sub to have
BYRef instead of BYVal, and the IDE says 'Set' parameter cannot be
declared as 'ByRef'.
Why?
How can I offer my class an object (any object) with out it taking a
copy to work with (byval).


First, basic rules:
ByVal passes a copy of the passed variable/expression.
ByRef passes a reference to the passed variable/expression.

A variable/expression that is a value type contains the object.
A variable/expression that is a reference type contains a reference to the
object.
Conclusion: The Collection is not copied when passed ByVal because it is
a reference type. Only the reference to the Collection is copied.
--
Armin

Nov 20 '05 #2

P: n/a
Hi Alex,

The key to this question is remembering that a variable which holds an
object <doesn't> hold an object!! It holds a pointer, or reference, to the
object. Thus you have two separate pieces of memory intimately associated. If
you make a copy of an object variable you are creating a new reference to the
same associated object - whether you do it by assignment or as a ByVal
parameter.

Consider this class:
Class Foo
Private m_Bar As SomeCollection

Property Bar As SomeCollection
Set
Bar = Value
End Set
End Property
End Class

In use:
Dim oFoo As New Foo
Dim oCollectionOfStuff As New SomeCollection
oFoo.Bar = oCollectionOfStuff

The property oBar has now given oFoo.m_Bar a <copy> of oCollectionOfStuff
because Value is passed <ByVal>.

oCollectionOfStuff, however, holds a <reference> to an instance of
SomeCollection. So m_Bar holds a <copy of the reference>.

This means that there are there are now two references but still only a
single instance of SomeCollection. (There's no duplication of the contents of
the colection either.)

==========================
ByVal and ByRef in association with objects is the same, and yet not the
same, as ByVal and ByRef with ValueTypes, such as Ints, Structures, etc.

The similarity with ByVal is that <in both cases> it will pass a <copy> of
the variable.
The difference is that with ValueTypes, it's a copy of the actual data,
whereas for object types it's a copy of the <reference> and hence there's no
duplication of the object itself (just the referencing variable).

The similarity with ByRef is that <in both cases> it will pass a
<reference> to the variable.
The difference is that with ValueTypes, it's a refernce to the the actual
data, so changes to the variable within the method will produce changes in the
caller's variable.
But for object types it's a reference to the <reference> and changes to
the variable within the method will cause the caller's variable to point to a
different object. [The original object will remain unchanged, but may find
itself without anyone pointing to it, and will thus be ready for garbage
collection.]

Regards,
Fergus
Nov 20 '05 #3

P: n/a
Hi Alex, a Collection object is a reference type, so you're not passing a
copy of the Collection around, rather just passing the 32-bit heap pointer
to the collection object around.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Alex Stevens" <al**@matrixinfotech.co.uk_nospam> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
Hi.

I have a class and it exposes a property, which accepts a parameter
collection object.
I want the class to use the parameter object and update it.
However I don't want to use a copy of the collection.
So I innocently open up my class, and modify the Set sub to have BYRef
instead of BYVal, and the IDE says 'Set' parameter cannot be declared as
'ByRef'.
Why?
How can I offer my class an object (any object) with out it taking a copy to work with (byval).

Thanks

Alex

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.