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

A

Alex

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
 
G

Guest

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
 
P

Peter Rilling

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.
 
J

Jon Skeet [C# MVP]

Alex said:
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
 
M

Manoj G [MVP]

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.
 
J

Jeff Louie

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top