471,354 Members | 1,169 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Creating a class that is "readonly" for some classes?

Hello,

Is it possible to have a class that has "readonly" properties for some
classes and can be written by others?

The reason I want this is that some of my classes need to expose their
"status", but I need to be ensure that this is only updated by the class
that "owns" them, and is not updateable by anything else. Also, I'd rather
these classes pass their "status" by reference instead of by value, so any
other object querying this "status" will have an up-to-date view of the
value.

The status of the classes I mentioned is not a simple string or enum, but
rather a more complex type, such as

class Status
{
string Name = "N/A";
string TypeName = "N/A";
enState State = enState.Unknown;
int MessagesProcessed = 0;
etc...
}

Right now I have something like (forgive the pseudocode)...

class Worker1 : WorkerBase
{
private Status m_Status = new Status();

Worker1()
{
// do work - private methods will update values within "m_Status"
object
}

public Status MyStatus
{ get { return m_Status; } }
}

I hope I'm making sense! Let me know if I need to clarify anything...

Cheers,
Alex
Nov 27 '05 #1
5 1414
You could either make the set accessors of the properties internal or create
some sort of securitty mechanism where the class outputs its key (byte[]) at
creation:
mc = new MyClass(out key)
mc.Unlock();
//modify it, is read-write
mc.Lock();
//now readonly
That way, only code that knows the key will be able to modify the properties
of the class. Hope this helps.
Reuben
Nov 27 '05 #2
You have to do it yourself. Make a check on each entrance into the object
properties.

There might be lots of ways of doing this. One way might be to make the
properties "internal" and place them in their own assembly. All classes
that need to write would be located in this assembly. Other ways might be
to wrap create another object that wraps the main object that can write to
the object, maybe using inheritance. Ultimately, you class may need at
least a reference to its parent/owner so that it can make a comparison.

Also, classes are always passed by references.

"Alex" <no****@hotmail.com> wrote in message
news:43***********************@news.zen.co.uk...
Hello,

Is it possible to have a class that has "readonly" properties for some
classes and can be written by others?

The reason I want this is that some of my classes need to expose their
"status", but I need to be ensure that this is only updated by the class
that "owns" them, and is not updateable by anything else. Also, I'd rather
these classes pass their "status" by reference instead of by value, so any
other object querying this "status" will have an up-to-date view of the
value.

The status of the classes I mentioned is not a simple string or enum, but
rather a more complex type, such as

class Status
{
string Name = "N/A";
string TypeName = "N/A";
enState State = enState.Unknown;
int MessagesProcessed = 0;
etc...
}

Right now I have something like (forgive the pseudocode)...

class Worker1 : WorkerBase
{
private Status m_Status = new Status();

Worker1()
{
// do work - private methods will update values within "m_Status"
object
}

public Status MyStatus
{ get { return m_Status; } }
}

I hope I'm making sense! Let me know if I need to clarify anything...

Cheers,
Alex

Nov 27 '05 #3
Alex <no****@hotmail.com> wrote:
Is it possible to have a class that has "readonly" properties for some
classes and can be written by others?
A couple of ways of doing this:

1) Put the class in an assembly with only the other classes which are
allowed to modify the values. Then make the "setter" methods internal.

2) Make the classes which need to modify the status nested classes
within the status class itself. That way they have access to the
private members of Status.
The reason I want this is that some of my classes need to expose their
"status", but I need to be ensure that this is only updated by the class
that "owns" them, and is not updateable by anything else. Also, I'd rather
these classes pass their "status" by reference instead of by value, so any
other object querying this "status" will have an up-to-date view of the
value.


You need to be very careful about the phrase "pass by reference" - I
suspect you actually just need reference type semantics, without
anything needing to be passed by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 27 '05 #4
Lets see if this works out for you:

1) Have the status class and classes which update it in one assembly.
2) You can have an internal method/setter so that classes which own it (are
in the same assembly) can set the a new value.
3) Have a public method or property which returns a cloned copy of the
status object which the rest of the classes can query. You need to have
value semantics here as other objects should not be able to set certain
properties on the actual instance of the status class.

--
HTH,
Manoj G
MVP, Visual Developer
http://msmvps.com/manoj

"Alex" <no****@hotmail.com> wrote in message
news:43***********************@news.zen.co.uk...
Hello,

Is it possible to have a class that has "readonly" properties for some
classes and can be written by others?

The reason I want this is that some of my classes need to expose their
"status", but I need to be ensure that this is only updated by the class
that "owns" them, and is not updateable by anything else. Also, I'd rather
these classes pass their "status" by reference instead of by value, so any
other object querying this "status" will have an up-to-date view of the
value.

The status of the classes I mentioned is not a simple string or enum, but
rather a more complex type, such as

class Status
{
string Name = "N/A";
string TypeName = "N/A";
enState State = enState.Unknown;
int MessagesProcessed = 0;
etc...
}

Right now I have something like (forgive the pseudocode)...

class Worker1 : WorkerBase
{
private Status m_Status = new Status();

Worker1()
{
// do work - private methods will update values within "m_Status"
object
}

public Status MyStatus
{ get { return m_Status; } }
}

I hope I'm making sense! Let me know if I need to clarify anything...

Cheers,
Alex

Nov 27 '05 #5
Write a wrapper class that contains a private reference to status and
provide read only methods using "containment by reference." Return an
instance of this wrapper class in GetStatus.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 27 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by XIAOLAOHU | 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.