473,382 Members | 1,611 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,382 software developers and data experts.

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

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.


Jun 27 '08 #1
7 1346
On 2008-05-29, Andy B <a_*****@sbcglobal.netwrote:
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 :)
--
Tom Shelton
Jun 27 '08 #2
Makes sense. This brings up another question: When do you use the "ByVal"
keyword?
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
On 2008-05-29, Andy B <a_*****@sbcglobal.netwrote:
>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 :)
--
Tom Shelton

Jun 27 '08 #3
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

"Andy B" <a_*****@sbcglobal.netwrote in message
news:ex**************@TK2MSFTNGP06.phx.gbl...
Makes sense. This brings up another question: When do you use the "ByVal"
keyword?

Jun 27 '08 #4
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.

On Thu, 29 May 2008 19:46:23 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>Makes sense. This brings up another question: When do you use the "ByVal"
keyword?
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
>On 2008-05-29, Andy B <a_*****@sbcglobal.netwrote:
>>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 :)
--
Tom Shelton
Jun 27 '08 #5
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" <jj******@cinnovations.netwrote in message
news:or********************************@4ax.com...
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.

On Thu, 29 May 2008 19:46:23 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>>Makes sense. This brings up another question: When do you use the "ByVal"
keyword?
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
>>On 2008-05-29, Andy B <a_*****@sbcglobal.netwrote:
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 :)
--
Tom Shelton

Jun 27 '08 #6
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.

On Thu, 29 May 2008 21:04:24 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>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" <jj******@cinnovations.netwrote in message
news:or********************************@4ax.com.. .
>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.

On Thu, 29 May 2008 19:46:23 -0400, "Andy B" <a_*****@sbcglobal.net>
wrote:
>>>Makes sense. This brings up another question: When do you use the "ByVal"
keyword?
"Tom Shelton" <to*********@YOUKNOWTHEDRILLcomcast.netwrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
On 2008-05-29, Andy B <a_*****@sbcglobal.netwrote:
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 :)
--
Tom Shelton
Jun 27 '08 #7
On May 29, 9:04 pm, "Andy B" <a_bo...@sbcglobal.netwrote:
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" <jjack...@cinnovations.netwrote in message

news:or********************************@4ax.com...
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.
On Thu, 29 May 2008 19:46:23 -0400, "Andy B" <a_bo...@sbcglobal.net>
wrote:
>Makes sense. This brings up another question: When do you use the "ByVal"
keyword?
>"Tom Shelton" <tom_shel...@YOUKNOWTHEDRILLcomcast.netwrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
On 2008-05-29, Andy B <a_bo...@sbcglobal.netwrote:
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 :)
>--
Tom Shelton
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]
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gry | last post by:
I often find myself wanting an instance attribute that can take on only a few fixed symbolic values. (This is less functionality than an enum, since there are no *numbers* associated with the...
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
3
by: Robert | last post by:
I have an application with custom configuration sections in it's app.config file. Here's a shortened excerpt: <monitors> <monitor...
0
by: mlafarlett | last post by:
Geez...seems like i've compared every friggin character...works n this programA but not n programB. I have a config file with multiple sections and when trying to read each section, it works great...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
4
balabaster
by: balabaster | last post by:
Okay, I decided that I needed to understand the whole custom configuration file bits and so I've spent some time playing around with it. It seems (in the most) relatively straight forward. However,...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.