Jump to content
  • Advertisement
Sign in to follow this  
Thevenin

Readonly members of an array or list.

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a book, which is basically either an array of pages, or a List<> of pages. The pages are intended to be READONLY; I don't want it to be possible to compile MyBook[2] = null;
public abstract cPages[] MyBook
{
get;
}
Essentially, what I want to be able to do exactly is:
public abstract List<readonly cPages> MyBook
{
get;
}
How should I code this?

Share this post


Link to post
Share on other sites
Advertisement
If you want the array to be read-only, use a read-only collection wrapper such as System.Collection.ObjectModel.ReadOnlyCollection (you might be better rolling up your own to support generics). More easily, make your property return a IEnumerable<Foo>

If you want the items of the array to be read-only, then its a bit more complex. Your List<readonly cPages> makes me think you come from a C++ background. There's no such feature in C#, you'll have to do a wrapper for your cPages class that only has getters, and then maybe a wrapper over your collection that will enable to iterate over your readonly item wrappers instead of the items themselves.

Its a bit of work, but there's no easier way, AFAIK.

Share this post


Link to post
Share on other sites
Interfacing IEnumerable<> seems like more work than I'm willing to commit to this very small section of a much larger project.

I know that it is impossible to do what I want to do (at least in C#), but I'm hoping for a more convenient workaround than defining a subclass that has a "this[int index]" on it.

Personally, I perfer the subclass than interfacing IEnumerable.


public class cBook
{
string[] MyPages;

public cBook(string[] ThePages)
{
MyPages= ThePages;
}

public cBook this[int TheIndex]
{
get
{
return MyPages[TheIndex];
}
}

public int MyCount
{
get
{
return MyPages.Length;
}
}
}


However, "ReadOnlyCollection<T>" looks EXACTLY like what I want.

Thanks! [grin]
ratings++;

P.S.: I think since .NET Framework 2.0, there has been a generic implementation of ReadOnlyCollection.

Share this post


Link to post
Share on other sites
It may or may not be what you want.

The elements in a ReadOnlyCollection<T> can still be altered by calling mutating member functions or set properties. If you want the equivilent of a C++ const type, you'll need a new class that provides no mutating member functions or set properties.

Share this post


Link to post
Share on other sites
You could use List<T>.AsReadOnly(), which will return a ReadOnlyCollection<T> for you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!