473,405 Members | 2,287 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,405 software developers and data experts.

ReadOnly Properties - Arguements For/Against

Hi all,

Ok - I guess this one will eventually still end up as a "personal choice"
kinda answer, but I thought I'd run it passed the wealth of knowledge and
experience that's in this group.

Typically when I'm writing my classes I'll have a series of properties which
I set to be read only, I'll then, if I need to write some methods for
setting the values of the class level variables exposed in the readonly
properties.

I appreciate I could remove the word "ReadOnly" and use the "Set" method of
the property but I've always felt that it would be better to have something
explicit for setting the values so that another developer in our team would
be fully aware when they are setting these values...

For example

' class variable
Private _name As String

' properties
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property

' methods
Public Sub SetName(ByVal value As String)

' set variable
_name = value

End Sub
Obviously in the above example I could just as easily use the property to
set the value instead of writing a separate method as its only doing very
simple things and with no big consequences.

I would be interested to know from others whether or not you do anything
similar or just use the properties to both get and set values.

Typically my classes themselves set the values, but every now and then they
will be the need for something else to set them, hence the sub being public
etc...

Any info appreciated.

Regards

Rob


Dec 5 '05 #1
5 1085
I disagree with your approach simply because it doesn't jive with
conventional tactics. I can't say why it's convential or not, it simply is
and therefore you've increased the learning curve of your programs and
provided no advantages for doing so. I always learnt that methods were
about behaviour and properties about state, ur code seems to mix this up
unecessarily.

In general, the only time I use properties are for identity keys (shouldn't
be able to set a UserId) and collections (because that's plain'ol good
design).

Take a look at for more insight (there's plenty of comments I haven't read,
so you might find arguments supporting your point)
http://blogs.msdn.com/brada/archive/.../13/50672.aspx

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

"Rob Meade" <ku***************@edaem.bor> wrote in message
news:eu****************@TK2MSFTNGP10.phx.gbl...
Hi all,

Ok - I guess this one will eventually still end up as a "personal choice"
kinda answer, but I thought I'd run it passed the wealth of knowledge and
experience that's in this group.

Typically when I'm writing my classes I'll have a series of properties
which I set to be read only, I'll then, if I need to write some methods
for setting the values of the class level variables exposed in the
readonly properties.

I appreciate I could remove the word "ReadOnly" and use the "Set" method
of the property but I've always felt that it would be better to have
something explicit for setting the values so that another developer in our
team would be fully aware when they are setting these values...

For example

' class variable
Private _name As String

' properties
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property

' methods
Public Sub SetName(ByVal value As String)

' set variable
_name = value

End Sub
Obviously in the above example I could just as easily use the property to
set the value instead of writing a separate method as its only doing very
simple things and with no big consequences.

I would be interested to know from others whether or not you do anything
similar or just use the properties to both get and set values.

Typically my classes themselves set the values, but every now and then
they will be the need for something else to set them, hence the sub being
public etc...

Any info appreciated.

Regards

Rob

Dec 5 '05 #2
What makes you think that :

MyObject.Name="SomeValue"

is not explicit ?

Actually it's perhaps even more explicit as you see the assignment operator
(while your replacement is just a method call and you guess from the name
that it actually updates the Name property).

--
Patrice

"Rob Meade" <ku***************@edaem.bor> a écrit dans le message de
news:eu****************@TK2MSFTNGP10.phx.gbl...
Hi all,

Ok - I guess this one will eventually still end up as a "personal choice"
kinda answer, but I thought I'd run it passed the wealth of knowledge and
experience that's in this group.

Typically when I'm writing my classes I'll have a series of properties which I set to be read only, I'll then, if I need to write some methods for
setting the values of the class level variables exposed in the readonly
properties.

I appreciate I could remove the word "ReadOnly" and use the "Set" method of the property but I've always felt that it would be better to have something explicit for setting the values so that another developer in our team would be fully aware when they are setting these values...

For example

' class variable
Private _name As String

' properties
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property

' methods
Public Sub SetName(ByVal value As String)

' set variable
_name = value

End Sub
Obviously in the above example I could just as easily use the property to
set the value instead of writing a separate method as its only doing very
simple things and with no big consequences.

I would be interested to know from others whether or not you do anything
similar or just use the properties to both get and set values.

Typically my classes themselves set the values, but every now and then they will be the need for something else to set them, hence the sub being public etc...

Any info appreciated.

Regards

Rob

Dec 5 '05 #3
Hi,

If the item to get or set behaves like a variable from the perspective of
someone thinking of your class as a black box -- what you set it to is the
same thing you'd expect to get back, although there could be some internal
logic involved -- then it is perfectly suited to be a property with get/set
accessors.

If the item is being set by deriving it from something else, or some other
appreciable action (from the same black box perspective) is being performed
in connection with setting it, then your suggested approach is appropriate.
--
Chris Priede
Dec 5 '05 #4
Hi,

"> I would be interested to know from others whether or not you do anything
similar or just use the properties to both get and set values.
I usually stick to properties because that way all get/set related code is
in one place. Just personal preference.
Typically my classes themselves set the values, but every now and then
they will be the need for something else to set them, hence the sub being
public etc...


Yes, that's also a reason to use separate method if the access level of the
entire property cannot be public etc. In .NET v2.0 you can have different
access levels for get/set in properties so that solves that part of the
issue.
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Dec 5 '05 #5
Thanks all for your replies, deffo some food for thought...

Cheers

Rob
Dec 5 '05 #6

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

Similar topics

19
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering...
14
by: Codemonkey | last post by:
Just a silly question, but why can't you overload a writeonly property with a readonly one? Surely the compiler can tell the difference of which one to call at compile time, depending on if it is...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
7
by: DareDevil | last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by...
1
by: Jumping Matt Flash | last post by:
I'm trying to achieve a property within a class which returns a conditional result dependent on the class members values. I will explain.. Public Class Address Private addrCountry As String...
7
by: Jimbo | last post by:
Hi, I am trying to set the readonly attribute of a text input tag dynamically from javascript. The input element already exists in the form and I want to make it readonly when a particular...
1
by: Niclas | last post by:
Hi, I am developing a service in WCF and am returing a complex object to the consumer of the service. I would like some properties of this object to be readonly but from what I understand...
1
by: Brad Pears | last post by:
Perfect. That makes more sense then calling a method to run the Stored Proc and return the value in the read only properties "Get" method because then it would be running that everytime someone...
0
by: Simon Woods | last post by:
Hi I was struggling to find a suitable place to ask this question, so if it is not appropriate here and someone can suggest somewhere else please do. My question is concerning IOC containers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.