New at VB

  • Thread starter Thread starter Stephen Martinelli
  • Start date Start date
S

Stephen Martinelli

Im not sure it this is the right newsgroup but.......can someone expain to
me when the get statement fires in a property procedure.

Example
Property lastname as string
Get
LastName=mstrLastName
End Get
Set(ByVal Value as String)
mstrLastName = Value
End Set
End Property

When i remark out the assignment within the get statement, the programs
still executes

Help

Steve
 
* "Stephen Martinelli said:
Im not sure it this is the right newsgroup but.......can someone expain to
me when the get statement fires in a property procedure.

Example
Property lastname as string
Get
LastName=mstrLastName
End Get
Set(ByVal Value as String)
mstrLastName = Value
End Set
End Property

When i remark out the assignment within the get statement, the programs
still executes

The 'Property Get' will be called when getting the property's value:

\\\
Dim x As New Foo()
x.LastName = "Herfried" ' Calls Set.
Dim s As String = x.LastName ' Calls Get.
///
 
Hi Stephen,

A Get isn't obliged to return a value. If you don't specify one, the
default for its value type will be returned.

Get
LastName = mstrLastName
End Get
is effectively the same as
Get
Dim LastName As String
LastName = mstrLastName
End Get
but you can't comment out the Dim because it's 'hidden'

Regards,
Fergus
 
* (e-mail address removed) (Herfried K. Wagner [MVP]) scripsit:
The line above doesn't make sense. Use 'Return mstrLastName'
instead. The code above will call the 'Property Set' again.
 
Herfried,
The line above doesn't make sense. Use 'Return mstrLastName'
instead.
I totally agree with both statements, it doesn't make sense, and you should
use the return statement!
The code above will call the 'Property Set' again.
Actually it doesn't!

As Fergus explained. VB.NET has this 'feature' that allows you to use the
function/property name for the return value, instead of using Return. Which
also means you cannot declare a parameter or local variable the same name as
your function.

I find using Return to be much more obvious on what you are doing.

Hope this helps
Jay
 
* "Jay B. Harlow said:
Actually it doesn't!

As Fergus explained. VB.NET has this 'feature' that allows you to use the
function/property name for the return value, instead of using Return. Which
also means you cannot declare a parameter or local variable the same name as
your function.

I find using Return to be much more obvious on what you are doing.

You (and Fergus) are right (I don't see Fergus' reply for some reason).
I always used 'Return' in VB.NET, so I forgot about this "feature"
inherited from VB Classic.

;-)
 
Hi Herfried,

ROFL. I complain that the gnus web site is impenetrable and look at the
revenge it takes!!

Regards,
Fergus
 
Any time a calling routine reference a property in a way that will read the
value from the property, the Get routine fires. So, if you do this:

MsgBox(myObj.LastName)

it will fire the Get routine (because the value is being "read"). On the
other hand, whenever you "set" a property value, the Set routine is run. So
if you do this:

myObj.LastName = "Smith"

it will fire the Set routine.

The line of code you have in the Get statement (LastName=mstrLastName) just
results in the property doing what a programmer would expect a property to
do when he/she references it (namely, return the value "stored there").
But, you don't have to do this - and furthermore, you can do anything else
you like here. For instance, you could count the number of times a property
is read (I have no idea why anyone would want to count such a thing, but
it's just an example so cut me some slack). If you did something like this
you would increment a numeric variable in the Get routine.


I hope this answers your question,

Eric
 
Hi Eric,

|| For instance, you could count the number of
|| times a property is read

Yep.

|| I have no idea why anyone would want to count
|| such a thing, but it's just an example

So that you can determine out of the dozens of just-in-case properties you
have created, which ones your users are actually using.

|| so cut me some slack

Precisely! Any property found slacking gets cut.

;-)))

Regards,
Fergus
 
Herfried,
I use OE, not gnus.

Interesting that it won't show his post.

I wonder how many others turn up "missing".

Jay
 
* "Fergus Cooney said:
ROFL. I complain that the gnus web site is impenetrable and look at the
revenge it takes!!

I think gnus is a newsreader for Linux hackers.
 
Back
Top