473,804 Members | 4,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1344
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*****@oblivi on.com> wrote in message
news:z2cfd.2222 2$%k.21245@pd7t w2no... 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.nestedo bj 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*****@oblivi on.com> wrote in message
news:z2cfd.2222 2$%k.21245@pd7t w2no...
| 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
1414
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
1019
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
5063
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 can modify the object no matter how it's defined (ByVal or ByRef). In some cases, I want to make sure that you cannot modify the object. Is there a way to do that in VS2003 or the up comming VS2005?
13
1767
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 properties, I have to store it back into the session object to "update" it, right? Or will the changes to my object automatically be saved back into the session object? Thanks, Jerry
1
5563
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 DetailsView displaying the selected company from dridview. I'm able to use the DetailsView to insert and update members of the site (companies), but I have this problem with a FileUpload, which is bound to a
26
5116
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 whenever user clones the window, I'll be able identify this and provide an extra session.
12
7505
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 it to reset info in a combo box. Below is some sample code for my view interface and the presenter: public interface IDevToolView
2
1159
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 how to easily generate the form? By properties I mean the business object looks like: public class BusinessObject { public string prop1; public string prop2;
4
1234
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 from the original place and read it in python and process it. #---------------------------- world = World(name='MyWorld')
19
2449
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; //every class may have this statement self.hello = function() {
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.