Results 1 to 2 of 2

Thread: Collection

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    29

    Collection

    I have a class (called ConnectionCollection) that implements IList and ICollection
    And i have also another class called ConnectionManager.

    Only the ConnectionManager is allowed to Add, Insert and Remove objects to the ConnectionCollection. Other classes may only view the objects with the Item property (or by using an enumerator).
    How can i make this possible?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Collection

    You can't expose the ConnectionCollection outside the ConnectionManager class if that's the case. Usually you would have a setup like this:
    VB Code:
    1. Public Class ConnectionManager
    2.     Private _connections As ConnectionCollection
    3.  
    4.     Public ReadOnly Property Connections() As ConnectionCollection
    5.         Get
    6.             If Me._connections Is Nothing Then
    7.                 Me._connections = New ConnectionManager
    8.             End If
    9.  
    10.             Return Me._connections
    11.         End Get
    12.     End Property
    13.  
    14. End Class
    You will not be able to do that though because then the caller would be able to call the methods of the ConnectionCollection object. Instead you must completely conceal the collection, which you can do like this:
    VB Code:
    1. Public Class ConnectionManager
    2.     Private _connections As ConnectionCollection
    3.  
    4.     Public ReadOnly Property Connections(ByVal index As Integer) As Connection
    5.         Get
    6.             If Me._connections Is Nothing Then
    7.                 Me._connections = New ConnectionManager
    8.             End If
    9.  
    10.             Return Me._connections(index)
    11.         End Get
    12.     End Property
    13.  
    14. End Class
    Now all the caller can do is get a reference to a specific Connection object from the collection by providing an index. The property is ReadOnly so they cannot assign a new object to that index and they have no access to the collection itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width