Get/Set sections of a class property confusing...

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

I saw this in the set accessor of a property:

Set(ByVal value As DataSet)



What exactly does the stuff in the () mean? VS complained about it not being
there when I took it out not knowing it needed to be there.
 
I saw this in the set accessor of a property:

Set(ByVal value As DataSet)



What exactly does the stuff in the () mean? VS complained about it not being
there when I took it out not knowing it needed to be there.

Well... Under the covers, get and set accessors are methods. The
argument to the Set part of the property, is the value you want to pass
in. Properties are syntactic sugar, really.

Basically this:

Class SomeClass
Private _data As DataSet

Public Property Data As DataSet
Get
Return Me._data
End Get
Set (ByVal value As DataSet)
Me._data = value
End Set
End Property
End Class

Dim c As SomeClass = New SomeClass()
c.Data = New DataSet()

Turns into is something like (warning, this is psuedo code for
illustration purposes only!):

Class SomeClass
Private _data As DataSet

Public Function GetData () As DataSet
Return Me._data
End Function

Public Sub SetData (ByVal value As DataSet)
Me._data = value
End Sub
End Class

Dim c As SomeClass = New SomeClass()
c.SetData (New DataSet())

Of course, the compiler immits IL, not basic code - but that is
essentially what the il is doing. It is creating a get/set pair of
methods and calling them :)
 
Andy, this looks like it would be a good time for you to get a good basic
book on VB and spend a few days studying it. It'd be a lot faster than
asking one question at a time.

Tom Dacon
Dacon Software Consulting
 
99.999% of the time you will use the default of ByVal.

With ByVal, changes made to the parameter are not reflected in the
caller. With ByRef, changes made to the parameter are seen by the
caller.

Consider these methods:

Public Sub MyMethod1(ByVal ds As DataSet)
ds = Nothing
End Sub

Public Sub MyMethod2(ByRef ds As DataSet)
ds = Nothing
End Sub

Dim ds As New DataSet

MyMethod1(ds)
' ds still has a reference to the DataSet
' allocated in the Dim statement

MyMethod2(ds)
' ds is now Nothing

It is important to distinguish the parameter from properties of the
object being passed. ByVal does not prevent the called method from
changing properties of the object.
 
Makes sense. so, using ByRef makes anything outside the method/property see
the new value of the object when the method gets done with it. The other is
only changed for the scope of the method/property. Properties of the passed
object can be changed for the external code even when ByVal is used.



Jack Jackson said:
99.999% of the time you will use the default of ByVal.

With ByVal, changes made to the parameter are not reflected in the
caller. With ByRef, changes made to the parameter are seen by the
caller.

Consider these methods:

Public Sub MyMethod1(ByVal ds As DataSet)
ds = Nothing
End Sub

Public Sub MyMethod2(ByRef ds As DataSet)
ds = Nothing
End Sub

Dim ds As New DataSet

MyMethod1(ds)
' ds still has a reference to the DataSet
' allocated in the Dim statement

MyMethod2(ds)
' ds is now Nothing

It is important to distinguish the parameter from properties of the
object being passed. ByVal does not prevent the called method from
changing properties of the object.
 
Yes. ByRef can be used as a way to return more information than just
using the return value of a function. However, I don't like to do
that because it may not be obvious to someone reading the code that
some parameters to a method call may be modified by the call.
 
Makes sense. so, using ByRef makes anything outside the method/property see
the new value of the object when the method gets done with it. The other is
only changed for the scope of the method/property. Properties of the passed
object can be changed for the external code even when ByVal is used.

Actually things get even more confusing when you throw in value and
reference types. Value types (such as integers) when used as ByRef
parameters will be changed by the method, but when you use ByVal they
are actually copied by the function and the original will not change.
For reference types however (such as classes) only the pointer is
passed ByRef/ByVal, so in essence the original object (the true object
the pointer is pointing at) will always be modified, making ByRef and
ByVal seemingly pointless when it comes to reference types.

Sorry, I'm sure that sounded rather confusing, especially since I
typed it very fast :-)

Thanks,

Seth Rowe [MVP]
 
Back
Top