Home All Groups Group Topic Archive Search About

Function versus ReadOnly Property

Author
7 Jun 2006 8:29 PM
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

Author
7 Jun 2006 8:39 PM
Jim Wooley
Show quote Hide quote
> 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).
Author
7 Jun 2006 9:08 PM
Ken Tucker [MVP]
Hi,

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

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

Show quoteHide quote
"eric.gofo***@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
>
>
Author
8 Jun 2006 2:55 AM
GhostInAK
Hello eric.gofo***@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

Show quoteHide quote
> 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
Author
8 Jun 2006 5:38 AM
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

<eric.gofo***@gmail.com> schreef in bericht
Show quoteHide quote
news:1149712149.601589.128150@j55g2000cwa.googlegroups.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
>
Author
8 Jun 2006 9:37 AM
Larry Lard
eric.gofo***@gmail.com wrote:
Show quoteHide quote
> 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
Author
8 Jun 2006 1:30 PM
Herfried K. Wagner [MVP]
<eric.gofo***@gmail.com> schrieb:
Show quoteHide quote
> 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/>
Author
8 Jun 2006 11:08 PM
david
On 2006-06-07, eric.gofo***@gmail.com <eric.gofo***@gmail.com> wrote:
Show quoteHide quote
> 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?
Author
11 Jun 2006 3:27 PM
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]

Show quoteHide quote
"david" <da***@woofix.local.dom> schreef in bericht
news:slrne8hbkd.gdb.david@localhost.localdomain...
> On 2006-06-07, eric.gofo***@gmail.com <eric.gofo***@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?
>
>
Author
11 Jun 2006 5:50 PM
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.

Show quoteHide quote
"Michel Posseth [MCP]" <M***@posseth.com> wrote in message
news:Oecu0tWjGHA.4040@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:slrne8hbkd.gdb.david@localhost.localdomain...
> > On 2006-06-07, eric.gofo***@gmail.com <eric.gofo***@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?
> >
> >
>
>
>
Author
12 Jun 2006 9:02 AM
Larry Lard
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
Author
12 Jun 2006 10:28 AM
Cor Ligthert [MVP]
Larry,

Show quoteHide quote
>
> 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/microsoft.public.dotnet.languages.vb/browse_frm/thread/974e13b6efdc0715/7e7401cc7316d84e#7e7401cc7316d84e

Cor
Author
14 Jun 2006 11:52 AM
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


Show quoteHide quote
"Michel Posseth [MCP]" <M***@posseth.com> wrote in message
news:Oecu0tWjGHA.4040@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:slrne8hbkd.gdb.david@localhost.localdomain...
| > On 2006-06-07, eric.gofo***@gmail.com <eric.gofo***@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?
| >
| >
|
|