Function versus ReadOnly Property

E

eric.goforth

Is there any reason to use:

Private newPropertyValue As Integer
Public ReadOnly Property MyProperty(ByRef MyParam as Integer) As
Integer
Get
Return newPropertyValue
End Get
End Property

instead of:

Dim MyValue as Integer
Public Function MyFunction(ByRef MyParam as Integer) As Integer
Return MyValue
End Function
 
J

Jim Wooley

Is there any reason to use:
Private newPropertyValue As Integer
Public ReadOnly Property MyProperty(ByRef MyParam as Integer) As
Integer
Get
Return newPropertyValue
End Get
End Property
instead of:

Dim MyValue as Integer
Public Function MyFunction(ByRef MyParam as Integer) As Integer
Return MyValue
End Function

One practical reason is that ASP can't bind to a function (or at least it
couldn't, haven't played with ASP 2.0 enough if this is no longer the case)
 
G

Guest

Hi,

In addition to Jim comments I really dont think it is good form to
have a property that has a parameter.

Ken
 
G

GhostInAK

Hello (e-mail address removed),

As a general rule of thumb, use a property when
1. You do not need to pass parameters (unless of course the data type is
a collection/list and the parameter is the item index)
2. You do not need to do any processing

If conditions 1 or 2 fail then use a function.

-Boo
 
C

Cor Ligthert [MVP]

Eric,

Functions do not what properties do by instance they are not serialized do
not show up in property boxes.

In my idea therefore in a class that will be used more times there should be
ever used forever properties.
In by instance a login showdialog window I do not see direct the sense.

Just my 2 Euro cents.

Cor
 
L

Larry Lard

Is there any reason to use:

Private newPropertyValue As Integer
Public ReadOnly Property MyProperty(ByRef MyParam as Integer) As
Integer
Get
Return newPropertyValue
End Get
End Property

instead of:

Dim MyValue as Integer
Public Function MyFunction(ByRef MyParam as Integer) As Integer
Return MyValue
End Function

Any particular reason you've used ByRef in these snippets? ByVal should
be your preferred default.
 
H

Herfried K. Wagner [MVP]

Is there any reason to use:

Private newPropertyValue As Integer
Public ReadOnly Property MyProperty(ByRef MyParam as Integer) As
Integer
Get
Return newPropertyValue
End Get
End Property

instead of:

Dim MyValue as Integer
Public Function MyFunction(ByRef MyParam as Integer) As Integer
Return MyValue
End Function


That's hard to say based on the sample you posted. IMO the decision between
a function and a property should be driven by the semantics of the member.
Attributes of an entity (class) should be implemented as properties and
operations the entity can perform should be implemented as methods.
However, there are some other practical guidelines too:

* Setting or getting a property should not involve time and resource
consuming operations.

* Setting or getting a properts should not change an object's state
except the property value. In other words, setting or getting a
property value should not have any hard-to-see side-effects.

VB.NET 2002/2003 do not support declaring the 'Get' and 'Set' accessors of a
property with different scopes. So sometimes it's necessary to implement
either 'Set' or 'Get' as a function to archieve different scopes for 'Set'
and 'Get'.
 
D

david

Is there any reason to use:

Private newPropertyValue As Integer
Public ReadOnly Property MyProperty(ByRef MyParam as Integer) As
Integer
Get
Return newPropertyValue
End Get
End Property

instead of:

Dim MyValue as Integer
Public Function MyFunction(ByRef MyParam as Integer) As Integer
Return MyValue
End Function

In addition to Herfried's comments, I'd add that Properties really
shouldn't take parameters except for simple keys and indexes. Otherwise
function syntax makes a lot more sense.

BTW, why is MyParam declared ByRef in the above?
 
M

Michel Posseth [MCP]

I am currently working in a project team where everyone is passing
parameters byref
i told them about the possible bugst this might raise but they said , it is
obvious to me what is happening and byref is faster as byval so ....

well euhhh ,,, yeah .... point ......

if someone has a good counter to this logic be my guest


regards

Michel Posseth [MCP]
 
M

Michael D. Ober

The performance difference between byref and byval is negligible. You
should always use ByVal for clarity and code correctness. Unfortunately,
until you can show a code bug as a result of byref, you won't win this
argument. As for using functions vs. ReadOnly properties, it really depends
on what syntax you're looking for. Note that properties won't let you use
byref.

Mike Ober.

Michel Posseth said:
I am currently working in a project team where everyone is passing
parameters byref
i told them about the possible bugst this might raise but they said , it is
obvious to me what is happening and byref is faster as byval so ....

well euhhh ,,, yeah .... point ......

if someone has a good counter to this logic be my guest


regards

Michel Posseth [MCP]

david said:
In addition to Herfried's comments, I'd add that Properties really
shouldn't take parameters except for simple keys and indexes. Otherwise
function syntax makes a lot more sense.

BTW, why is MyParam declared ByRef in the above?
 
L

Larry Lard

Michel said:
I am currently working in a project team where everyone is passing
parameters byref
i told them about the possible bugst this might raise but they said , it is
obvious to me what is happening and byref is faster as byval so ....

Is it? Have they checked? Can they prove it?
if someone has a good counter to this logic be my guest

I tend to struggle to deal logically with people who are prepared to
break semantic contracts for illusory (and even if not illusory,
negligible) performance gains.
 
C

Cor Ligthert [MVP]

Larry,
Is it? Have they checked? Can they prove it?


I tend to struggle to deal logically with people who are prepared to
break semantic contracts for illusory (and even if not illusory,
negligible) performance gains.

I was writing the same, but did not, it would not help Michel at all.

In my ideas shows it a very big gap in knowledge with the people Michel has
to deal with while he probaly knows himself all in and outs.

Here is by the way a messagethread which is in my idea very complete.

http://groups.google.com/group/micr...3b6efdc0715/7e7401cc7316d84e#7e7401cc7316d84e

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Michel,
I would recommend you & your team review:

http://www.yoda.arachsys.com/csharp/parameters.html

Yes its C#, however the concepts on what ByRef (reference) & ByVal (value)
parameters are verses Reference types & Value types. Unfortunately far too
many developers confuse ByRef parameters with Reference Types.


I recommend only using ByRef when you actually need ByRef, that is when you
intend on changing the caller's variable itself. Anything else is simply
misleading & confusing programming.

Remember that ByVal & ByRef are how you pass parameters, Reference Types &
Value types are have values are stored...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|
| I am currently working in a project team where everyone is passing
| parameters byref
| i told them about the possible bugst this might raise but they said , it
is
| obvious to me what is happening and byref is faster as byval so ....
|
| well euhhh ,,, yeah .... point ......
|
| if someone has a good counter to this logic be my guest
|
|
| regards
|
| Michel Posseth [MCP]
|
| "david" <[email protected]> schreef in bericht
| | >> Is there any reason to use:
| >>
| >> Private newPropertyValue As Integer
| >> Public ReadOnly Property MyProperty(ByRef MyParam as Integer) As
| >> Integer
| >> Get
| >> Return newPropertyValue
| >> End Get
| >> End Property
| >>
| >> instead of:
| >>
| >> Dim MyValue as Integer
| >> Public Function MyFunction(ByRef MyParam as Integer) As Integer
| >> Return MyValue
| >> End Function
| >
| > In addition to Herfried's comments, I'd add that Properties really
| > shouldn't take parameters except for simple keys and indexes. Otherwise
| > function syntax makes a lot more sense.
| >
| > BTW, why is MyParam declared ByRef in the above?
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top