Strong Typed Collection Classes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Strong Typed Collection Classes

  1. #1
    Marcel Gnoth Guest

    Strong Typed Collection Classes

    Hi ,

    I want simply create a objectmodell in VB7. One Collection of "products"
    that contains "product" Objects and their Keys.
    I want access this Collection with "products.item(Key)" and (if now is
    chrismass) "products.item(index)". Also it would be very nice if I can use
    the for each construct.

    In the very Beta MSDN I found that TypedDictionaryBase and
    TypedCollectionBase are for this case. I don't want implement my own
    Collection Class. I will use Inheritance like in the example at the end

    If I then want access this collection class via for each then i got a
    InvalidCastExeptiion because the For each gives me Objects of type
    "DictionaryEntry" and with DictionaryEntry.Value I got a MyStruct Object but
    I want write "For Each xS In dicStruct" like in vb6

    Protected Sub cmdProductShowAll_Click(ByVal sender As Object, ByVal e As
    System.EventArgs)
    Dim xS As MyStruct
    lvwProducts.ListItems.Clear()
    For Each xS In dicStruct
    lvwProducts.ListItems.Add(xs.Name)
    Next
    End Sub


    How can I made my own typesafe Collection classes via Inheritance? If I must
    create my own Collection class wich intern structure should I use?
    ArrayList, Dictionary, ObjectList,....?


    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Imports System.Collections.Bases

    Public Structure MyStruct
    Public Name As String
    Public Birthday As Date
    End Structure

    Public Class MyTestDictionary
    Inherits TypedDictionaryBase

    Public Property Item(ByVal Key As String) As MyStruct
    Get
    Return CType(Dictionary.Item(Key), MyStruct)
    End Get
    Set
    Dictionary.Item(Key) = Value
    End Set
    End Property

    Public Sub Add(ByVal Key As String, ByVal Value As MyStruct)
    Dictionary.Add(Key, Value)
    End Sub

    Public Sub Remove(ByVal Key As String)
    Dictionary.Remove(Key)
    End Sub
    End Class
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    bye Marcel



  2. #2
    Franklin E. Vargas Guest

    Re: Strong Typed Collection Classes


    Greetings Marcel,

    It appears that you've answered your own question here. You know already
    that the collection you've opted to use is a collection of dictionary entries--that
    is, of key and value pairs. If in your case you are only interested in values,
    then you must use the <collection>.Values function. Here's a quick snapit.

    dim Item as object
    for each Item in myList.Values
    System.Console.WriteLine(Item.GetType().FullName)
    next Item




    "Marcel Gnoth" <mgnoth@nteam.de> wrote:
    >Hi ,
    >
    >I want simply create a objectmodell in VB7. One Collection of "products"
    >that contains "product" Objects and their Keys.
    >I want access this Collection with "products.item(Key)" and (if now is
    >chrismass) "products.item(index)". Also it would be very nice if I can use
    >the for each construct.
    >
    >In the very Beta MSDN I found that TypedDictionaryBase and
    >TypedCollectionBase are for this case. I don't want implement my own
    >Collection Class. I will use Inheritance like in the example at the end
    >
    >If I then want access this collection class via for each then i got a
    >InvalidCastExeptiion because the For each gives me Objects of type
    >"DictionaryEntry" and with DictionaryEntry.Value I got a MyStruct Object

    but
    >I want write "For Each xS In dicStruct" like in vb6
    >
    > Protected Sub cmdProductShowAll_Click(ByVal sender As Object, ByVal

    e As
    >System.EventArgs)
    > Dim xS As MyStruct
    > lvwProducts.ListItems.Clear()
    > For Each xS In dicStruct
    > lvwProducts.ListItems.Add(xs.Name)
    > Next
    > End Sub
    >
    >
    >How can I made my own typesafe Collection classes via Inheritance? If I

    must
    >create my own Collection class wich intern structure should I use?
    >ArrayList, Dictionary, ObjectList,....?
    >
    >
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    >Imports System.Collections.Bases
    >
    >Public Structure MyStruct
    > Public Name As String
    > Public Birthday As Date
    >End Structure
    >
    >Public Class MyTestDictionary
    > Inherits TypedDictionaryBase
    >
    > Public Property Item(ByVal Key As String) As MyStruct
    > Get
    > Return CType(Dictionary.Item(Key), MyStruct)
    > End Get
    > Set
    > Dictionary.Item(Key) = Value
    > End Set
    > End Property
    >
    > Public Sub Add(ByVal Key As String, ByVal Value As MyStruct)
    > Dictionary.Add(Key, Value)
    > End Sub
    >
    > Public Sub Remove(ByVal Key As String)
    > Dictionary.Remove(Key)
    > End Sub
    >End Class
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    >
    >bye Marcel
    >
    >



  3. #3
    Marco Antonio Pérez López Guest

    Re: Strong Typed Collection Classes

    Hi, Marcel,

    "Marcel Gnoth" <mgnoth@nteam.de> escribió en el mensaje
    news:3b1d4ced@news.devx.com...
    > Hi ,
    >
    > I want simply create a objectmodell in VB7. One Collection of "products"
    > that contains "product" Objects and their Keys.


    I'm having the very same problem, only that I'm trying to do it on C# which
    is more or less the same. If you want to use access like in VB both by index
    and key, you will have to use the NamedObjectCollectionBase to inherit from.
    TypedCollectionBase only let you access by int index, TypedDictionaryBase
    only let you access by string key.

    Now I'm getting the same InvalidCast Exception. If you want the ForEach to
    work, I guess you don't have other solution than to define your own
    IEnumerator in your class. I posted a question very similar to yours in
    csharp.general (under thread "Collection classes revisited") and I'm waiting
    for some answer; I think it's a waste to have to write so much code when in
    VB6 you just hit the Class Wizard and specify that you want to create a
    typed class of objects Foo named Foos and voilá. I'll keep you informed.

    Marco.





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center