Results 1 to 5 of 5

Thread: for each sorted list

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    for each sorted list

    Hey,

    I have the following code in a class that inherits from SortedList.

    VB Code:
    1. Public Sub Dispose() Implements System.IDisposable.Dispose
    2.         Dim entry As DictionaryEntry
    3.         For Each entry In MyBase.Values
    4.             CType(entry.Value, Invoice).Dispose()
    5.         Next
    6.     End Sub

    The for each entry in mybase.value causes the following error
    System.InvalidCastException - Specified cast is not valid.

    Does anyone know why this is happening? I thought that Values return a dictionary entry.

    Thanks,
    Don't anthropomorphize computers -- they hate it

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: for each sorted list

    VB Code:
    1. Public Sub Dispose() Implements System.IDisposable.Dispose
    2.         For Each Inv as Invoice In MyBase.Values
    3.             Inv.Dispose()
    4.         Next
    5. End Sub

  3. #3

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: for each sorted list

    Thanks a lot. That worked fine.
    I'm wondering though, how come the dictionary entry didn't work when the help file states that with teh for each loop you should use the dictionary entry.
    Don't anthropomorphize computers -- they hate it

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: for each sorted list

    i don't know, but possibly because when you inherit the sorted list and put your own objects in the collection, the collection consists of objects of type invoice, and not of type DictionaryEntry

  5. #5

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: for each sorted list

    The collection contains keys and values. But I was iterating through the Values collection which contained objects of type Invoice, so that's why I was getting that error.

    Thanks a lot,
    Don't anthropomorphize computers -- they hate it

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