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

How to make an object property truly read-only?

Don
If you expose an object as a property in a VB.NET class, like so:
public class MyClass
private obj as NestedClass
Public Readonly Property NestedObj as OtherClass
Get
return obj
End Get
End Property
End Class
You can't modify the object directly, but you can still modify members of
the readonly property(!):

Dim myobj as MyClass
myobj.NestedObj = New NestedClass ' Doesn't work
myobj.NestedObj.Whatever = "hello" ' Works! And the changes stick!
Is there anyway to prevent this?

- Don
Nov 21 '05 #1
2 1311
It depends on how far you want to take this.
If you truly don't want the underlying object to be modified, you can create
a new object with the appropriate values and return that. They could modify
the values of the new object, but the underlying object would remain
unchanged. This is the easy solution, but can be confusing for the caller as
your sample below would still "work", sort of.
myobj.NestedObj.Whatever = "hello" would not generate any sort of error, but the change would be discarded
immediately as the returned object immediately goes out of scope.

Dim tmpObj as New NestedClass
tmpObj = myobj.NestedObj
tmpObj.Whatever = "hello"

No error, and tmpObj is changed to "hello", however myobj.NestedObj is
unchanged.

If you need to make it more friendly to the user, you need to deal with the
scope of the properties of the nested object.

Gerald

"Don" <un*****@oblivion.com> wrote in message
news:z2cfd.22222$%k.21245@pd7tw2no... If you expose an object as a property in a VB.NET class, like so:
public class MyClass
private obj as NestedClass
Public Readonly Property NestedObj as OtherClass
Get
return obj
End Get
End Property
End Class
You can't modify the object directly, but you can still modify members of
the readonly property(!):

Dim myobj as MyClass
myobj.NestedObj = New NestedClass ' Doesn't work
myobj.NestedObj.Whatever = "hello" ' Works! And the changes stick!
Is there anyway to prevent this?

- Don

Nov 21 '05 #2
you build scope access from the bottom up and not the other way around. this
is as it should be. readonly for the nestedobj is correct...you cannot set
myclass.nestedobj to anything. if you want to have different access at the
nestedclass property level, then that is where you should define it. right
now, that is kind of awkward since vb.net won't support different property
access combinations until vs.net 2k5 (ex., public get with a friend set).

hth,

steve
"Don" <un*****@oblivion.com> wrote in message
news:z2cfd.22222$%k.21245@pd7tw2no...
| If you expose an object as a property in a VB.NET class, like so:
|
|
| public class MyClass
| private obj as NestedClass
| Public Readonly Property NestedObj as OtherClass
| Get
| return obj
| End Get
| End Property
| End Class
|
|
| You can't modify the object directly, but you can still modify members of
| the readonly property(!):
|
| Dim myobj as MyClass
| myobj.NestedObj = New NestedClass ' Doesn't work
| myobj.NestedObj.Whatever = "hello" ' Works! And the changes stick!
|
|
| Is there anyway to prevent this?
|
| - Don
|
|
Nov 21 '05 #3

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

Similar topics

0
by: archway | last post by:
Hi, I would like to build a hierarchy of ProductNode objects like so ProductNode ---Product Node ---ProductNode ------ProductNode ------ProductNode ---ProductNode
2
by: Hal Heinrich | last post by:
I have the following class: Public Class myObj Dim x as myOtherObj Public Property otherObject() As myOtherObj Get otherObject = x End Get
20
by: Brien King | last post by:
If I have a parameter that has an Object type (as opposed to something like a string), can I make that parameter a CONST? Right now, if you pass an object into a sub/function, that sub/function...
13
by: | last post by:
Simple question, I think... I'm storing an object in the Session object. In the code behind I read that object: trx = CType(Session("Transaction"), BOCSTransaction) If I change any...
1
by: ThomasNexoe | last post by:
Hi there, I quite new to posting in this forum. I have a problem which I truly hope someone can help me out. I have a master/detail page - with a gridview displaying company names, and a...
26
by: mikharakiri_nospaum | last post by:
Is there a way to tell one window from another? A quick look into wndow object properties revealed nothing obvious. I want to keep track of window object identity in a hidden input widget, so that...
12
by: Doug | last post by:
Hi, I learned a little about the model view presenter pattern at a conference this last week and am experimenting with it. It's working pretty well but I have a question. I am trying to use...
2
by: james | last post by:
I recieve a business object and need to create a form to edit each property. This object is huge (like over 100 properties), and I dont want to make 100 label/textboxes by hand. Does anyone know...
4
by: jm.suresh | last post by:
Hi, Is there any standard text format for storing data of object oriented nature. The text file should be readable. That is, Is there any better way than having to write out a file like this...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.