472,135 Members | 1,295 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,135 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 1260
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by archway | last post: by
2 posts views Thread by Hal Heinrich | last post: by
20 posts views Thread by Brien King | last post: by
13 posts views Thread by | last post: by
26 posts views Thread by mikharakiri_nospaum | last post: by
2 posts views Thread by james | last post: by
19 posts views Thread by zzw8206262001 | 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.