473,657 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3685
"Alex Stevens" <al**@matrixinf otech.co.uk_nos pam> 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 oCollectionOfSt uff As New SomeCollection
oFoo.Bar = oCollectionOfSt uff

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

oCollectionOfSt uff, 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.Reflecti on Master "

==== Converting to 2002 ====
Remove inline declarations
"Alex Stevens" <al**@matrixinf otech.co.uk_nos pam> wrote in message
news:uU******** ******@TK2MSFTN GP10.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
5302
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: Public Property Let dicReplaceVars(byref vdicReplaceVars) set p_ReplaceVars = vdicReplaceVars End Property
6
9056
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
5726
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 following is a diskette class (vb .net) that works find, in that I can validate a diskette is mounted, dismount it, lock it, unlock it, get diskette geometry, etc., all with a valid handle from CreateFile API! I can even position the file pointer,...
7
2472
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 the user It then dawned on me that I was passing in a readonly property into the method yet neither at compile time or runtime was I getting any kind of error or warning. I tested with a simple string field and it alter the string as expected but...
2
2316
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 variable in the method, when the method exits the variable is unchanged. Here is some code: Module Module1 Sub Main()
2
10265
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 application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not Public Class ITest Private MyName As String Public TestId As String Public Sub New()
3
1903
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
2893
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 below get a cMyObject as ByRef or ByVal? Thanks. Public ReadOnly Property MyObject() As cMyObject Get Return moMyObject End Get End Property
5
2094
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 performance reasons for using ByRef rather than ByVal? In my (tiny) mind, I see ByRef as passing a reference to an already created object, I can then use values of that object or do things to it, where-as ByVal creates a separate instance of it...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6167
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.