473,398 Members | 2,404 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,398 software developers and data experts.

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

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
3 3653
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ryan Malone | last post by:
Passing Dictionary object byref Ive created an ASP class that uses a dictionary object which is filled from a recordset. It passes the object to the propterty of another ASP class byref: ...
6
by: Cc | last post by:
hi, is there a way to use byref on property set , because i would like to pass the value into the variable byref ?
2
by: Schorschi | last post by:
Can't seemd to get ReadFile API to work! Returns invalid handle error? =========================================================================== Ok, the visual basic gurus, help! The...
7
by: DareDevil | last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by...
2
by: Terry Heath | last post by:
Hi - I've tried different ways to write to the variable me, but with no luck. I've tried passing it to other methods as a byref parameter, and even though it looks like it's written to the...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
3
by: doriengard | last post by:
Dear VB gurus, A long question, thanks for your patience in advance :-) Part 1) I have a VB windows application that contains a start-up form: frmMain
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.