C # application collection forms

I am using .Net 2.0 and it is driving me crazy, but there is probably something lightweight that I am not doing that I am too confused to see right now.

I have an application that has a collection of custom-made items, only the main form needs to be able to modify the contents of that collection, and all other forms need to be able to read from it.

There is actually only one other form, but I need it so that it can be called multiple times when it edits different items in the custom collection. Therefore, to make a dialog box with a child element impractical. You need to be able to open multiple instances of this form containing different items from the collection.

I just can't imagine how to get all forms to revolve around the same collection instance. As I said, I was probably missing something obvious, but I stopped thinking about the problem properly.

EDIT: Yikes! I couldn't explain myself very well. All subforms should be able to read and write the elements of the collection, but I only want to use one instance of the collection at a time while the program is running.

EDIT 2: It turned out that I needed a singleton. I have currently implemented a similar solution in another project. Thanks to the commenter below who didn't actually leave an answer, I can mark it as correct even if he nailed it.

0


source to share


3 answers


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

FROM#

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

      

V. B.

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

      



You can then reference your collection from your form by referencing MyCollection or _MyCollection of your choice, but outside of your form, the collection will be ReadOnly and therefore not editable.

Edit: After editing, you will see that you are using a singleton as stated earlier, does that mean that all instances of your forms should be able to edit this collection or not? If so, put your collection in 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 a collection is referenced from one of your forms, it will create a new collection from which you can add / remove items. Every time you reference a collection from this or one of your other forms, it has already been created, so it will return the collection that was originally created. However, none of the forms will be able to set the new collection, it just refers to the instance created by the singleton template.

+3


source


Check Method List<T>.AsReadOnly()

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



eg.

private LIst<Foo> fooList; 

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

      

+1


source


Do all forms need read / write access to the collection? So your only problem is how to give your child a link to this collection? Can't you pass it to the child's form constructor? Or make it a public property of your main form (which your child can apparently get a link from from their properties Owner

)? Or: shudder: make it a public static property?

0


source







All Articles