views:

560

answers:

3

I'm using .Net 2.0 and this is driving me crazy but there's probably some easy thing I'm not doing that I am now too confused to see.

I have an application that has a bespoke collection of objects in it, only the main form should be able to change the contents of that collection and all other forms should be able to read from it.

Actually there's only one other form but I need it to have the capability of being called multiple times as it edits different items in the bespoke collection. To make the child form a dialog is therefore impractical. The ability to open multiple instances of this form containing different items from the collection is necessary.

I just can't seem to think of a way of making all forms revolve around the same instance of the collection. Like I say I'm probably missing something obvious but I have ceased to be able to think about the problem properly.

EDIT: Yikes! I can't have explained myself very well. All subforms should be able to read and write collection items, but I want to only use one instance of the collection at a time whilst the programme is running.

EDIT 2: It turned out that what I needed was a singleton. I have implemented a similar solution in a different project now. Thanks to the commenter below who didn't actually leave an answer I can mark as the correct one, even though they nailed it.

+3  A: 

If the main form needs read/write access but other forms don't, then I would make the collection a property of your main form that is read/write from within your form, but read only from outside your form. You can do this using something like:

C#

private myCollection _MyCollection; 
public myCollection MyCollection { 
    get { return _MyCollection.AsReadOnly(); } 
    private set { _MyCollection = value; } 
}

VB

Private _MyCollection As myCollection
Public Property MyCollection() As myCollection
  Get
     Return _MyCollection.AsReadOnly
  End Get
  Private Set(ByVal value As myCollection)
    _MyCollection = value
  End Set
End Property

Then you can reference your collection from inside your form by referencing either MyCollection or _MyCollection as you choose, but from outside your form, the collection will be ReadOnly and therefore not editable.

Edit: After your edit, it does look like what you're after is a singleton as previously suggested does this mean that all instances of your forms should be able to edit this collection or not? If so, then put your collection into a static class:

Private _MyCollection as myCollection = Nothing
Public Shared ReadOnly Property MyCollection() As myCollection
   Get
      If _MyCollection is Nothing Then _MyCollection = New myCollection
      Return _MyCollection
   End Get
End Property

Now the first time the collection is referenced from within one of your forms it will instantiate a new collection which you can then add/remove items from. Every time you reference the collection from this or one of your other forms, it will already have been instantiated, so it will return the collection that was originally instantiated. None of the forms however will have the ability to set a new collection, just reference the one instantiated by the singleton pattern.

BenAlabaster
@Anthony: it'll be fine. Note the ".AsReadOnly()" calls.
Joel Coehoorn
This one gets the accepted as it mentions the Singleton.
+1  A: 

Check out the method List<T>.AsReadOnly()

This allows you to return a read-only wrapper for the list.

e.g.

private LIst<Foo> fooList; 

public ReadOnlyCollection<Foo> Foo { 
    get { return fooList.AsReadOnly(); }
Anthony
A: 

All forms need read/write access to the collection? So is your only issue now how to give your child forms a reference to this collection? Can't you pass it to the constructor of the child form? Or make it a public property of your main form (which your child forms can presumably get a reference to from their Owner properties)? Or, :::shudder::: make it a public static property?

P Daddy
The Owner thing sounds good, I'll give that a go...