I use something like this:
Public NotInheritable Class myCollection
Implements ICollection
Private collectArray As string() = {}
Private CT as integer
Sub New()
Ct = 0
End Sub
Default Overloads ReadOnly Property Item(ByVal index As Integer) As
MultiTableStyle
Get
If index >= 0 AndAlso index <= Ct - 1 AndAlso Ct >= 1 Then
Return collectArray(index)
Else
Return Nothing
End If
End Get
End Property
Public Sub Add(ByVal newString As String)
collectArray(Ct) = newstring
If UBound(collectArray) <= Ct Then ReDim Preserve
collectArray(UBound(collectArray) + CT)
collectArray(Ct) = newStyle
Ct = Ct + 1
End Sub
Public Overloads Sub Remove(ByVal indexToRemove As Integer)
End Sub
'The IsSynchronized Boolean property returns True if the collection is
designed to be thread safe; otherwise, it returns False.
Public ReadOnly Property IsSynchronized() As Boolean Implements
ICollection.IsSynchronized
Get
Return False
End Get
End Property
'The SyncRoot property returns an object, which is used to synchronize
'the collection. This should return the instance of the object or return the
'SyncRoot of another collection if the collection contains other collections.
'
Public ReadOnly Property SyncRoot() As Object Implements ICollection.SyncRoot
Get
Return Me
End Get
End Property
'The ReadOnly property Count returns the number of items in the custom
collection.
Public ReadOnly Property Count() As Integer Implements ICollection.Count
Get
Return Ct
End Get
End Property
Public Function GetEnumerator() As IEnumerator Implements
ICollection.GetEnumerator
Return New MultiTableStylesEnumerator(collectArray)
End Function
Private Sub CopyTo(ByVal myArr As Array, ByVal index As Integer) Implements
ICollection.CopyTo
'Dummy for Icollection - Not implemented in this Class
End Sub
End Class
'Enumerator for MyCollection Collection Class
Friend Class MultiTableStylesEnumerator
Implements IEnumerator
Private collectArray() As MultiTableStyle
Private Cursor As Integer
Sub New(ByVal collectArray() As MultiTableStyle)
Me.collectArray = collectArray
Cursor = -1
End Sub
Public Sub Reset() Implements IEnumerator.Reset
Cursor = -1
End Sub
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
If Cursor < collectArray.Length Then
Cursor = Cursor + 1
End If
If (Cursor = collectArray.Length) Then
Return False
Else
Return True
End If
End Function
Public ReadOnly Property Current() As Object Implements IEnumerator.Current
Get
If ((Cursor < 0) Or (Cursor = collectArray.Length)) Then
Throw New InvalidOperationException
Else
Return collectArray(Cursor)
End If
End Get
End Property
End Class
--
Dennis in Houston
"Erick" wrote:
I've created a class called Procs and a collection class called
Processes which uses a hastable object to store the Procs.
Now i want to enumerate with the "For each" to extract all the Procs in
my Processes class. As far as i can tell i need to implement an
IEnuerator method to do this. But how ?
'Procs Class
Public Class Procs
Private _Id As Integer
Private _AppName As String
Private _Owner As String
Private _StartTime As DateTime
Friend Property Id()
Get
Return _Id
End Get
Set(ByVal Value)
_Id = Value
End Set
End Property
Friend Property AppName()
Get
Return _AppName
End Get
Set(ByVal Value)
_AppName = Value
End Set
End Property
Friend Property Owner()
Get
Return _Owner
End Get
Set(ByVal Value)
_Owner = Value
End Set
End Property
Friend Property StartTime()
Get
Return _StartTime
End Get
Set(ByVal Value)
_StartTime = Value
End Set
End Property
End Class
'processes class
Imports System.Collections
Public Class Processes
Private _Processes As New Hashtable
Friend Function Add(ByVal Proc As Procs)
_Processes.Add(Proc.Id, Proc)
End Function
Friend Function Item(ByVal Id As Integer) As Procs
Return _Processes.Item(Id)
End Function
Public Function GetEnumerator() As IEnumerator
Return _Processes.GetEnumerator()
End Function
End Class
' test call from a form load event
Dim y As New Procs
Dim v As New Procs
Dim z As New Processes
y.Id = 1
y.AppName = "hello"
v.Id = 2
v.AppName = 2
z.Add(y)
z.Add(v)
Dim q As Procs
For Each q In z
MsgBox(q.Id + " " + q.AppName)
Next
The for each loop won't work because Ienumerate has not been
implemented. But I can't find the correct code to do this. Can some one
fill in the code ?
I'm using .net 1.1
Erick