Results 1 to 4 of 4

Thread: problem with objects

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    problem with objects

    Hi,

    i have the following class
    Code:
    Public Class room
        Public roomID As Integer
        Public hotelID As Integer
        Public size As String
        Public singleDouble As String
        Public enSuite As Boolean
        Public cost As Integer
        Public description As String
    End Class
    and the next class makes a collection of rooms
    Code:
    Public Class rooms
        Private mcolRooms As New Collection()
    
        'add a room to our collection
        Public Sub add(ByVal room As room, ByVal key As String)
            mcolRooms.Add(room, key)
        End Sub
    
        'return the number of rooms in our collection
        Public ReadOnly Property Count() As Integer
            Get
                Return mcolRooms.Count
            End Get
        End Property
    
        'return a room object from our collection
        Public ReadOnly Property Item(ByVal IndexOrKey As Object) As room
            Get
                Return mcolRooms.Item(IndexOrKey)
            End Get
        End Property
    
    End Class
    the problem i have is that i am trying to check to see if a room has been added to the collection already. This would be checked by the roomID(which is added as a key when the add procedure is used). Is there an easy way or do i have to loop through the collection and check each room to see if it exists?

    Thanks
    Nick

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well if you are using the collection object then there is no intrinsic way of finding out whether the item is there or not. A lot of the other collection variants can though. Or if you don't want to switch that then you can use error handling to find out the quick way.

    VB Code:
    1. Private Function ContainsKey(ByVal col As Collection, ByVal key As String) As Boolean
    2.         Try
    3.             Dim o As Object = col.Item(key)
    4.         Catch
    5.             Return False
    6.         End Try
    7.         Return True
    8.     End Function

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    Thanks Edneeis,

    that worked a treat. What are the other types of collections you mentioned though?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Check in the help under the System.Collection namespace there are quite a few: SortedList, Hashtable, Dictionary, NameValueCollection...

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