473,322 Members | 1,526 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,322 software developers and data experts.

Read-only properties returning reference types

I am not sure what the suggested practice is for returning reference types
from read-only properties. Consider an example: Suppose I have a Point
class that is a reference type. I use the Point class from within a Shape
class. I want the user of the Shape class to be able to see the location of
the Shape but not be able to change it. So I declare a read-only property:

class Shape {
private Point location;
/* other Shape stuff */

Point Location
{
get
{
return location;
}
}

/* ... */
}

However this has a problem. The get accessor returns a reference type, so
whoever accesses the property can use that reference type to modify the
private member variable:

Shape shape = new shape(...);
Point point = shape.Location;

// Modify internal member variable through returned reference!
point.x = newxvalue;

What are the recommended ways to circumvent this problem? I see two
solutions: 1) return a copy of the location Point class or 2) make each
field of location, x and y (and z?), a read-only property of Shape that
returns a value type. I think solution 2) could get out of hand with too
many properties being required. I am not sure if 1) is recommended or not.
All the examples I can find of creating read-only properties use only value
types so they do not answer my question.
Nov 15 '05 #1
2 2071
I think the way I would address this is to have the members of your Point
object be readonly, plus add some internal properties that offer write
access. This way they can be read and altered by Shape, but only read by
any client using the object.

"Cory Burkhardt" <bu********@hotmail.com.nospam> wrote in message
news:40***********************@news.twtelecom.net. ..
I am not sure what the suggested practice is for returning reference types
from read-only properties. Consider an example: Suppose I have a Point
class that is a reference type. I use the Point class from within a Shape
class. I want the user of the Shape class to be able to see the location of the Shape but not be able to change it. So I declare a read-only property:
class Shape {
private Point location;
/* other Shape stuff */

Point Location
{
get
{
return location;
}
}

/* ... */
}

However this has a problem. The get accessor returns a reference type, so
whoever accesses the property can use that reference type to modify the
private member variable:

Shape shape = new shape(...);
Point point = shape.Location;

// Modify internal member variable through returned reference!
point.x = newxvalue;

What are the recommended ways to circumvent this problem? I see two
solutions: 1) return a copy of the location Point class or 2) make each
field of location, x and y (and z?), a read-only property of Shape that
returns a value type. I think solution 2) could get out of hand with too
many properties being required. I am not sure if 1) is recommended or not. All the examples I can find of creating read-only properties use only value types so they do not answer my question.

Nov 15 '05 #2
Cory Burkhardt <bu********@hotmail.com.nospam> wrote:

<snip>
What are the recommended ways to circumvent this problem? I see two
solutions: 1) return a copy of the location Point class or 2) make each
field of location, x and y (and z?), a read-only property of Shape that
returns a value type. I think solution 2) could get out of hand with too
many properties being required. I am not sure if 1) is recommended or not.
All the examples I can find of creating read-only properties use only value
types so they do not answer my question.


2) is recommended *anyway* - you shouldn't (IMO) have any non-private
fields, in general.

Immutable objects are generally a good thing when they can fit into
your design easily - they make situations like this much simpler. If
the object is mutable, then you have to make a copy to be safe -
although within internal implementations, I usually find that
documenting what should and shouldn't be changed by the calling code to
be sufficient.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

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

Similar topics

6
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of...
0
by: munif | last post by:
i wnat write a C++ program that would read from a CD and display information about files and folders in the given CD In simple words you would be performing a simple (ls -l) or (DIR /S) on the...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
2
by: Andrea Bauer | last post by:
Hallo, wie kann ich so eine Datei unter .Net schreiben C++ oder C#. Bitte mit Funktionsaufrufen. Vielen Dank. Grüße Andrea <Product> <ProgramNumber>2</ProgramNumber>
4
by: Ollie Cook | last post by:
Hi, I am having some difficulty with read(2) and interrupting signals. I expect I am misunderstanding how the two work together, so would appreciate some guidance. I am trying to 'time out' a...
1
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
4
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
5
by: Thomas Christensen | last post by:
This issue has been raised a couple of times I am sure. But I have yet to find a satisfying answer. I am reading from a subprocess and this subprocess sometimes hang, in which case a call to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.