|
-
Dec 7th, 2006, 05:01 AM
#1
Thread Starter
Hyperactive Member
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:
Private m_Data As Collection
Public ReadOnly Property Data() As IEnumerable(Of ClsItem)
Get
Return m_Data
End Get
End Property
Using the IEnumerable(Of ClsItem) I though I would be able to do a for each like this;
VB Code:
Dim col As New ClsCol
'Add some items...
For Each item As ClsItem In col.Data
'Do something with 'item'
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."
-
Dec 7th, 2006, 10:36 AM
#2
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.
-
Dec 7th, 2006, 10:56 AM
#3
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:
Public Class Product
Private _ID As Integer = 0
Private _Desc As String = String.Empty
Public Sub New()
End Sub
Public Sub New(ByVal ID As Integer, ByVal Desc As String)
_ID = ID
_Desc = Desc
End Sub
Public Property ID() As Integer
Get
Return _ID
End Get
Set(ByVal value As Integer)
_ID = value
End Set
End Property
Public Property Desc() As String
Get
Return _Desc
End Get
Set(ByVal value As String)
_Desc = value
End Set
End Property
End Class
Public Class Products
Inherits System.Collections.CollectionBase
Default Public Overloads ReadOnly Property item(ByVal index As Integer) As Product
Get
Return DirectCast(MyBase.InnerList.Item(index), Product)
End Get
End Property
Default Public Overloads ReadOnly Property item(ByVal Desc As String) As Product
Get
For Each P As Product In MyBase.InnerList
If P.Desc = Desc Then Return P
Next
Return Nothing
End Get
End Property
Public Function Add(ByVal P As Product) As Integer
Return MyBase.InnerList.Add(P)
End Function
Public Function Add(ByVal ID As Integer, ByVal Desc As String) As Product
Dim P As New Product(ID, Desc)
MyBase.InnerList.Add(P)
Return P
End Function
Public Sub Remove(ByVal Desc As String)
For Each P As Product In MyBase.InnerList
If P.Desc = Desc Then
MyBase.InnerList.Remove(P)
Return
End If
Next
End Sub
Public Sub Remove(ByVal ID As Integer)
For Each P As Product In MyBase.InnerList
If P.ID = ID Then
MyBase.InnerList.Remove(P)
Return
End If
Next
End Sub
End Class
VB Code:
'USAGE
Dim MyProds As New Products
For i As Integer = 1 To 10
MyProds.Add(New Product(i, "Item Number " & i.ToString))
Next
For Each P As Product In MyProds
MessageBox.Show(P.Desc)
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.
-
Dec 7th, 2006, 05:33 PM
#4
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:
Public Class Thing
'Add members here.
End Class
Public Class ThingCollection
Inherits System.Collections.ObjectModel.Collection(Of Thing)
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).
-
Dec 7th, 2006, 05:47 PM
#5
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
-
Dec 7th, 2006, 05:57 PM
#6
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
-
Dec 8th, 2006, 03:14 AM
#7
Thread Starter
Hyperactive Member
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|