Readonly Property of type collection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
Iam writing an interface in c# , where two of the methods look like this -

ParameterCollection DependentProperties
{get;}

ParameterCollection IndependentProperties
{get;}


ParameterCollection = stronly typed collection of MyParameter objects.
MyParameter is a class with 3 string and 3 double properties.

I dont want the user to modify the DependentProperties collection, so I make
it readonly property, and in turn return a copy of the underlying collection.
But with the IndependentProperties property, I want the user to be able to
modify the objects contained in the collection (NOT modify the collection).

Currently, it works as follows -
I return a copy of the underlying collection, (and copy of objects contained
in it). The ParameterCollection class has a method, UpdateCollection that can
transfer values as follows:

void UpdateCollection(ParameterCollection pc)
{
for(i =0; i <this.count;i++)
this.Value = pc.Value;

// Where Value is a property of the MyParameter object, which is the only
one that can be changed by the user. The user cannot change other properties
of MyParameter objects, but uses them for display purposes.

}

My question is , is there a better way of doing this ?


thank you,
vikrantca
 
First of all I wouldn't return a copy, I'd return a reference. The fact that
you only implement a get ensures that the reference cannot be changed.
If the collection becomes big, all this copying may become expensive.

For the ParameterCollection I wouldn't implement a public Add() or Remove()
method. If these types are in a seperate assembly, you could mark them as
internal and they wouldn't be visible outside the assembly.

Another option would be to create a class ParameterCollectionInternal that
implements the Add() and Remove(), this class inherits from
ParameterCollection. In your code you use ParameterCollectionInternal, but
return the base type with less functionality to your user.

Chris
 
With regards to your first suggestion - I could return a reference, but then
the user could modify the state of the objects contained in the
ParameterCollection.
What do I do to prevent that ?

thanks
vikrantca

Christopher Kimbell said:
First of all I wouldn't return a copy, I'd return a reference. The fact that
you only implement a get ensures that the reference cannot be changed.
If the collection becomes big, all this copying may become expensive.

For the ParameterCollection I wouldn't implement a public Add() or Remove()
method. If these types are in a seperate assembly, you could mark them as
internal and they wouldn't be visible outside the assembly.

Another option would be to create a class ParameterCollectionInternal that
implements the Add() and Remove(), this class inherits from
ParameterCollection. In your code you use ParameterCollectionInternal, but
return the base type with less functionality to your user.

Chris


vikrantca said:
Hello,
Iam writing an interface in c# , where two of the methods look like this -

ParameterCollection DependentProperties
{get;}

ParameterCollection IndependentProperties
{get;}


ParameterCollection = stronly typed collection of MyParameter objects.
MyParameter is a class with 3 string and 3 double properties.

I dont want the user to modify the DependentProperties collection, so I
make
it readonly property, and in turn return a copy of the underlying
collection.
But with the IndependentProperties property, I want the user to be able to
modify the objects contained in the collection (NOT modify the
collection).

Currently, it works as follows -
I return a copy of the underlying collection, (and copy of objects
contained
in it). The ParameterCollection class has a method, UpdateCollection that
can
transfer values as follows:

void UpdateCollection(ParameterCollection pc)
{
for(i =0; i <this.count;i++)
this.Value = pc.Value;

// Where Value is a property of the MyParameter object, which is the only
one that can be changed by the user. The user cannot change other
properties
of MyParameter objects, but uses them for display purposes.

}

My question is , is there a better way of doing this ?


thank you,
vikrantca

 
Vikrantca... The standard approach is return an immutable collection
with getters, no setters. You create a null safe type safe collection
with getters and setters. You then wrap the get/set collection in a get
only class. You pass a reference to the appropriate class to the
appropriate user.

http://www.geocities.com/jeff_louie/OOP/oop7.htm

Regards,
Jeff
With regards to your first suggestion - I could return a reference, but
then the user could modify the state of the objects contained in the
ParameterCollection. What do I do to prevent that ?
 
I thought that was what you wanted:

"But with the IndependentProperties property, I want the user to be able to
modify the objects contained in the collection (NOT modify the
collection)."

If you only implement a getter, the user cannot change the property to point
to a different collection.

Chris




vikrantca said:
With regards to your first suggestion - I could return a reference, but
then
the user could modify the state of the objects contained in the
ParameterCollection.
What do I do to prevent that ?

thanks
vikrantca

Christopher Kimbell said:
First of all I wouldn't return a copy, I'd return a reference. The fact
that
you only implement a get ensures that the reference cannot be changed.
If the collection becomes big, all this copying may become expensive.

For the ParameterCollection I wouldn't implement a public Add() or
Remove()
method. If these types are in a seperate assembly, you could mark them as
internal and they wouldn't be visible outside the assembly.

Another option would be to create a class ParameterCollectionInternal
that
implements the Add() and Remove(), this class inherits from
ParameterCollection. In your code you use ParameterCollectionInternal,
but
return the base type with less functionality to your user.

Chris


vikrantca said:
Hello,
Iam writing an interface in c# , where two of the methods look like
this -

ParameterCollection DependentProperties
{get;}

ParameterCollection IndependentProperties
{get;}


ParameterCollection = stronly typed collection of MyParameter objects.
MyParameter is a class with 3 string and 3 double properties.

I dont want the user to modify the DependentProperties collection, so I
make
it readonly property, and in turn return a copy of the underlying
collection.
But with the IndependentProperties property, I want the user to be able
to
modify the objects contained in the collection (NOT modify the
collection).

Currently, it works as follows -
I return a copy of the underlying collection, (and copy of objects
contained
in it). The ParameterCollection class has a method, UpdateCollection
that
can
transfer values as follows:

void UpdateCollection(ParameterCollection pc)
{
for(i =0; i <this.count;i++)
this.Value = pc.Value;

// Where Value is a property of the MyParameter object, which is the
only
one that can be changed by the user. The user cannot change other
properties
of MyParameter objects, but uses them for display purposes.

}

My question is , is there a better way of doing this ?


thank you,
vikrantca

 
Back
Top