How: Public Read Only / Private Write Property? 
Author Message
 How: Public Read Only / Private Write Property?

Hi all,

Summary: Property should be ReadOnly publically, but Writable privately
(internally to the class).

In VB, we used to use separet Property Let and Property Get statements. This
has been much improved with the new combined property statement, but this
leaves me with a question.....

I have a 'MustInherit' class, which has an 'IsDirty' property. I would like
this property to be readable publicly, but only allow it to be written
privately. The only way I can see to do this is by using a ReadOnly
property, and then setting the underlying private, local variable (mIsDirty)
from within the class code. This means that I end up losing the local
encapsulation (biz-rules) and is not quite as nice as I would like it to be.

Obviously, I could set up a private method such as 'Private Sub
SetIsDirty(Value as boolean)' - but again this seems messier than I would
like.

Does anyone have a better solution to this.

Many thanks in advance as always!

Chris Keeble



Fri, 16 Jul 2004 18:06:56 GMT  
 How: Public Read Only / Private Write Property?
You could have two properties that act on the same data. Not quite as neat
as you'd like but should work ok.


Quote:
> Hi all,

> Summary: Property should be ReadOnly publically, but Writable privately
> (internally to the class).

> In VB, we used to use separet Property Let and Property Get statements.
This
> has been much improved with the new combined property statement, but this
> leaves me with a question.....

> I have a 'MustInherit' class, which has an 'IsDirty' property. I would
like
> this property to be readable publicly, but only allow it to be written
> privately. The only way I can see to do this is by using a ReadOnly
> property, and then setting the underlying private, local variable
(mIsDirty)
> from within the class code. This means that I end up losing the local
> encapsulation (biz-rules) and is not quite as nice as I would like it to
be.

> Obviously, I could set up a private method such as 'Private Sub
> SetIsDirty(Value as boolean)' - but again this seems messier than I would
> like.

> Does anyone have a better solution to this.

> Many thanks in advance as always!

> Chris Keeble



Sat, 17 Jul 2004 02:53:03 GMT  
 How: Public Read Only / Private Write Property?
Chris,
As messy as it is, I would go with the 'SetIsDirty(Value as Boolean)' or
maybe just 'SetDirty()' Although I like 'SetModified' more...

I would actually make the SetModified protected instead of private. So that
derived classes can use it...

Hope this helps
Jay


Quote:
> Hi all,

> Summary: Property should be ReadOnly publically, but Writable privately
> (internally to the class).

> In VB, we used to use separet Property Let and Property Get statements.
This
> has been much improved with the new combined property statement, but this
> leaves me with a question.....

> I have a 'MustInherit' class, which has an 'IsDirty' property. I would
like
> this property to be readable publicly, but only allow it to be written
> privately. The only way I can see to do this is by using a ReadOnly
> property, and then setting the underlying private, local variable
(mIsDirty)
> from within the class code. This means that I end up losing the local
> encapsulation (biz-rules) and is not quite as nice as I would like it to
be.

> Obviously, I could set up a private method such as 'Private Sub
> SetIsDirty(Value as boolean)' - but again this seems messier than I would
> like.

> Does anyone have a better solution to this.

> Many thanks in advance as always!

> Chris Keeble



Sat, 17 Jul 2004 10:44:43 GMT  
 How: Public Read Only / Private Write Property?
Thanks very much - much appreciated.

Chris



Quote:
> Chris,
> As messy as it is, I would go with the 'SetIsDirty(Value as Boolean)' or
> maybe just 'SetDirty()' Although I like 'SetModified' more...

> I would actually make the SetModified protected instead of private. So
that
> derived classes can use it...

> Hope this helps
> Jay



> > Hi all,

> > Summary: Property should be ReadOnly publically, but Writable privately
> > (internally to the class).

> > In VB, we used to use separet Property Let and Property Get statements.
> This
> > has been much improved with the new combined property statement, but
this
> > leaves me with a question.....

> > I have a 'MustInherit' class, which has an 'IsDirty' property. I would
> like
> > this property to be readable publicly, but only allow it to be written
> > privately. The only way I can see to do this is by using a ReadOnly
> > property, and then setting the underlying private, local variable
> (mIsDirty)
> > from within the class code. This means that I end up losing the local
> > encapsulation (biz-rules) and is not quite as nice as I would like it to
> be.

> > Obviously, I could set up a private method such as 'Private Sub
> > SetIsDirty(Value as boolean)' - but again this seems messier than I
would
> > like.

> > Does anyone have a better solution to this.

> > Many thanks in advance as always!

> > Chris Keeble



Tue, 20 Jul 2004 22:06:11 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Private and Public Properties of type Array

2. Best Practice: Referring to Public Properties or Private Variables

3. Public and private properties URGENT

4. Public and private properties URGENT

5. Placing an control in a class module as public or private property

6. Reading/Writing to Private INI file.

7. Accessing a Property through read-only and read-write interfaces

8. File Properties - Summary Tab - Read and Write Properties

9. Copy public calendar items to private calendars

10. How to distinguish public from private folders?

11. synchronize public calendar events to private calendars

12. Public Get Private Set

 

 
Powered by phpBB® Forum Software