Best way to set properties that need to be read only

M

Mike

I have a class that has another class as a property, MainClass and SubClass.
I will create the SubClass and then pass it too the MainClass in the
constructor. The SubClass has alot of properties I only have three in the
example below but there are actually 20 so I dont want to set them all in
the constructor of the SubClass. When the MainClass is accessed I want to
allow read only access to the SubClass that is a property of the MainClass.
I know I could make sub class a sruct, but I dont think that is the best
solution, there must be an accepted way to do this or my design is flawed.
Any Suggestions?

Thanks,

Mike

class MainClass
{
MainClass(string otherProperty1, SubClass subClass)
{
this.SubClass = subClass;
}

SubClass SubClass;
string OtherProperty1;
}

class SubClass
{
string Property1;
string Property2;
string Property3;
}
 
G

Guest

User readonly keyword of the C

I interpretted your problem in two ways so two solution

1. All properties of SubClass should be readonly to MainClass. In that case just define you

class SubClas

public readonly string Property1
public readonly string Property2
public readonly string Property3


2. SubClass the property of the MainClass should be readonl

In that case define your main class as follow
class MainClas

MainClass(string otherProperty1, SubClass subClass

this.SubClass = subClass


public readonly SubClass SubClass
string OtherProperty1
 
M

Mike

Gajanan

2 below is closer but If I understand this correctly it just prevents
setting the SubClass property on the Main class and will not prevent
changing the properties of the SubClass itself. Let me clarify this more
with some code:

SubClass subClass = new SubClass();
subClass.Property1 = "1";
subClass.Property2 = "2";
subClass.Property3 = "3";
subClass.Property4 = "4";

MainClass mainClass = new MainClass("1", subClass)

I want to allow:
string myString = mainClass.SubClass.Property1;

I want to prevent:
mainClass.SubClass.Property1 = "Some New Value";

One way would be to populate all properties from the constructor of SubClas
but there are more than 20 properties so that would be cumbersome, another
would be to use a struct but I think a class will be more efficient. I
understand that I could create local properties in MainClass to access the
subclass properties in a readonly fashion, but then I have to keep the 2
classes ion sync.

Thanks,

Mike
 
S

Steve Sargent

Hi:

I don't know if this will help or not, since I'm definitely not one of
the guru's who frequent this newsgroup. However, your question sounds
similar to a networking question I posted a while ago, so maybe the
solution would help you as well.

In a nutshell, why not create an Interface that allows only read
access to the properties you wish to have as readonly? Your actual
implementation could have write privileges on the class, but when
passed in to the parent class, it would only see the get property
methods.

I hope this idea is helpful to you. the technique worked well in my
situation.

Steve
 

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