Results 1 to 7 of 7

Thread: Return collection as IEnumerator

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425

    Return collection as IEnumerator

    Hi all,

    In my current situation I have two classes;
    1. ClsItem - one is the class with properties (like name and value)
    2. ClsCol - one is the class having a private collection object and storing the items with the type of class 1.

    What I want to do is implement a 'items' property that is used in the for each loops. Now, what I thought was this in the ClsCol;

    VB Code:
    1. Private m_Data As Collection
    2.     Public ReadOnly Property Data() As IEnumerable(Of ClsItem)
    3.         Get
    4.             Return m_Data
    5.         End Get
    6.     End Property

    Using the IEnumerable(Of ClsItem) I though I would be able to do a for each like this;
    VB Code:
    1. Dim col As New ClsCol
    2. 'Add some items...
    3. For Each item As ClsItem In col.Data
    4.     'Do something with 'item'
    5. Next

    However, the compiler says that I can not cast the m_Data as ClsItem in the Items property...

    Can anyone help me out here?

    Thanks.

    P.s. Using VS 2005
    Last edited by BShadow; Dec 7th, 2006 at 05:03 AM. Reason: Adding the VS 2005 info
    "Experience is something you don't get until just after you need 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: Return collection as IEnumerator

    If your collection class only supports types of ClsItem, howcome you are using generics? (IEnumerable(Of ClsItem))


    Just trying to understand what you have going on here to try to get you a solution.

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

    Re: Return collection as IEnumerator

    here is a pretty simple setup to give you an idea how to make a class, and a collection class for that class.

    VB Code:
    1. Public Class Product
    2.     Private _ID As Integer = 0
    3.     Private _Desc As String = String.Empty
    4.  
    5.     Public Sub New()
    6.  
    7.     End Sub
    8.  
    9.     Public Sub New(ByVal ID As Integer, ByVal Desc As String)
    10.         _ID = ID
    11.         _Desc = Desc
    12.     End Sub
    13.  
    14.     Public Property ID() As Integer
    15.         Get
    16.             Return _ID
    17.         End Get
    18.         Set(ByVal value As Integer)
    19.             _ID = value
    20.         End Set
    21.     End Property
    22.  
    23.     Public Property Desc() As String
    24.         Get
    25.             Return _Desc
    26.         End Get
    27.         Set(ByVal value As String)
    28.             _Desc = value
    29.         End Set
    30.     End Property
    31. End Class
    32. Public Class Products
    33.     Inherits System.Collections.CollectionBase
    34.  
    35.     Default Public Overloads ReadOnly Property item(ByVal index As Integer) As Product
    36.         Get
    37.             Return DirectCast(MyBase.InnerList.Item(index), Product)
    38.         End Get
    39.     End Property
    40.  
    41.     Default Public Overloads ReadOnly Property item(ByVal Desc As String) As Product
    42.         Get
    43.             For Each P As Product In MyBase.InnerList
    44.                 If P.Desc = Desc Then Return P
    45.             Next
    46.             Return Nothing
    47.         End Get
    48.     End Property
    49.  
    50.     Public Function Add(ByVal P As Product) As Integer
    51.         Return MyBase.InnerList.Add(P)
    52.     End Function
    53.     Public Function Add(ByVal ID As Integer, ByVal Desc As String) As Product
    54.         Dim P As New Product(ID, Desc)
    55.         MyBase.InnerList.Add(P)
    56.         Return P
    57.     End Function
    58.  
    59.     Public Sub Remove(ByVal Desc As String)
    60.         For Each P As Product In MyBase.InnerList
    61.             If P.Desc = Desc Then
    62.                 MyBase.InnerList.Remove(P)
    63.                 Return
    64.             End If
    65.         Next
    66.     End Sub
    67.     Public Sub Remove(ByVal ID As Integer)
    68.         For Each P As Product In MyBase.InnerList
    69.             If P.ID = ID Then
    70.                 MyBase.InnerList.Remove(P)
    71.                 Return
    72.             End If
    73.         Next
    74.     End Sub
    75. End Class

    VB Code:
    1. 'USAGE
    2.         Dim MyProds As New Products
    3.         For i As Integer = 1 To 10
    4.             MyProds.Add(New Product(i, "Item Number " & i.ToString))
    5.         Next
    6.         For Each P As Product In MyProds
    7.             MessageBox.Show(P.Desc)
    8.         Next

    you would likely want to implement some error handling and such in the code for production use, but this should give you a good idea on how to set things up.

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

    Re: Return collection as IEnumerator

    You're going about this in the wrong way. You should not ever use the Collection class for a start, but if you did then it IS an IEnumerable because Collection implements ICollection, which inherits IEnumerable. All you would have to do would be to cast it. Having said that, I can't see that there's a need to cast anyway because you can simply use any collection in a For Each loop as is.

    Secondly, kleinma's advice would be sound for .NET 1.1 but in .NET 2.0 you inherit System.Collections.ObjectModel.Collection(Of T) instead. By doing so you inherit the appropriate Add, Insert, Remove, Item, etc. members for free, instead of implementing them yourself as you would with CollectionBase, e.g.
    VB Code:
    1. Public Class Thing
    2.  
    3.     'Add members here.
    4.  
    5. End Class
    6.  
    7. Public Class ThingCollection
    8.     Inherits System.Collections.ObjectModel.Collection(Of Thing)
    9.  
    10. End Class
    That's pretty much all you need. Unless you need some special functionality you don't have to add anything to the ThingCollection class at all because it inherits all the standard functionality from Collection(Of Thing).
    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

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

    Re: Return collection as IEnumerator

    yeah I just moved over to 2.0 recently and haven't fully worked out all the details of using generics to simplify code.

    I will have to rework some of my code projects that I upgraded from 1.1 to 2.0 to use them

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

    Re: Return collection as IEnumerator

    Actually, I posted like I know what I'm talking about but I only became aware of that class yesterday myself. In my defence, I haven't tried to implement my own strongly-typed collection in 2.0 yet. Here's a link about collections posted by someone else in another thread I was participating in about generic collections and public properties: http://blogs.msdn.com/kcwalina/archi...llections.aspx
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425

    Re: Return collection as IEnumerator

    hi there,

    Thanks for the support. The reason I did not wat to send back the collection object itself, was that the collection has a collection of object and not a collection af a certain class type. I think that with Collection(Of T) this is solved? So I can delcare the private variable as
    VB Code:
    1. Private m_Data As New Collection(Of ClsItem)
    That would be great.

    --
    I've tried it and I get;
    'Microsoft.VisualBasic.Collection' has no type parameters and so cannot have type arguments.

    I also see that you meant inheritting the Collection(Of T). This would also expose some methods like add etc. This would be ok in this situation I think, but what if I didn't want those methods exposed, or want to name them differntly? That was the main idea when I wanted to implement the class myself and have a private variable declared as a Collection and handle all the methods on this collection myself...

    Any suggestions?

    Thx.
    Last edited by BShadow; Dec 8th, 2006 at 03:21 AM.
    "Experience is something you don't get until just after you need 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