MSDN Archive Home
Help and FAQs
MSDN Code Gallery Information and Discussion
All Resource Updates
Create New Discussion
A class having read-only private field, but public write access
Aug 29 2008 at 5:59 PM
Aug 29 2008 at 10:59 PM
This morning I ran across an interesting scenario and am clueless how to create it (or I haven't had my second Starbuck's quad venti). Anyway, I'm looking to create a local variable that is readonly privately, but I need public write access...
ReadOnly _ProfileMustExist As Boolean = True 'Defines it as read only, useless line of code really.
Public Property ProfileMustExistToReplace() As Boolean
Set(ByVal value As Boolean)
= value 'THIS LINES CAUSES A COMPILER ERROR, as it should. but I need to let be ok on this line only.
Basically, I only want to set _ProfileMustExist with the property/set and nowhere else in the code. This ensures that nowhere else in MyClass will _ProfileMustExist be set. This will stop any inadvertent setting of the variable and should throw a compiler if attempted. (as it does in my example).
My question, is there a way to override rule locally on one line only?
UPDATE (1): I think what I'm talking about is ReadOnly backing fields. Any ideas how to do?
UPDATE(2): Seems like the only place where _ProfileMustExist can be set is in the constructor ( the New() section ); but again, I would like to change the value as a property. Is there an Attribute I can assign that will allow the property to be set?
Sign in to post message or set email notifications
Manage Your Profile
MSDN Flash Newsletter
© 2008 Microsoft Corporation. All rights reserved.