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