471,073 Members | 1,142 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Function versus ReadOnly Property

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

Jun 7 '06 #1
12 2141
> 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).
Jun 7 '06 #2
Hi,

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

Ken
------------------

"er**********@gmail.com" wrote:
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

Jun 7 '06 #3
Hello er**********@gmail.com,

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
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

Jun 8 '06 #4
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

<er**********@gmail.com> schreef in bericht
news:11**********************@j55g2000cwa.googlegr oups.com...
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

Jun 8 '06 #5

er**********@gmail.com wrote:
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.

--
Larry Lard
Replies to group please

Jun 8 '06 #6
<er**********@gmail.com> schrieb:
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jun 8 '06 #7
On 2006-06-07, er**********@gmail.com <er**********@gmail.com> wrote:
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?
Jun 8 '06 #8

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" <da***@woofix.local.dom> schreef in bericht
news:sl******************@localhost.localdomain...
On 2006-06-07, er**********@gmail.com <er**********@gmail.com> wrote:
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?

Jun 11 '06 #9
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 [MCP]" <MS**@posseth.com> wrote in message
news:Oe**************@TK2MSFTNGP05.phx.gbl...

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" <da***@woofix.local.dom> schreef in bericht
news:sl******************@localhost.localdomain...
On 2006-06-07, er**********@gmail.com <er**********@gmail.com> wrote:
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?



Jun 11 '06 #10

Michel Posseth [MCP] wrote:
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.

--
Larry Lard
Replies to group please

Jun 12 '06 #11
Larry,

Michel Posseth [MCP] wrote:
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.


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/micro...7401cc7316d84e

Cor
Jun 12 '06 #12
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
"Michel Posseth [MCP]" <MS**@posseth.com> wrote in message
news:Oe**************@TK2MSFTNGP05.phx.gbl...
|
| 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" <da***@woofix.local.dom> schreef in bericht
| news:sl******************@localhost.localdomain...
| > On 2006-06-07, er**********@gmail.com <er**********@gmail.com> wrote:
| >> 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?
| >
| >
|
|
Jun 14 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Woody Splawn | last post: by
5 posts views Thread by fred | last post: by
27 posts views Thread by sklett | last post: by
49 posts views Thread by Emmett | last post: by
reply views Thread by leo001 | last post: by

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.