473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using For Each in a Custom collection class with hashtables

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

Oct 11 '06 #1
6 2739

Hi Eric,

You can use For Each on the Values or Keys collection of the hashtable.
Also, I'm not sure if you know about generics, but it's preferable to use
them instead of hashtables where you know the type of object you are
storing. So, for enumeration:
For Each theKey as Integer In MyHashtable.Keys

Next

For Each theValue As MyObject In MyHashtable.Values

Next


For Generics, look up the Dictionary type. You can then make a Dictionary
(of Key, Value ), which is better for type checking at compile time.



Oct 11 '06 #2

Sorry, a simpler explaination would be to expose the Values collection of
the private hashtable you have there and iterate that:

For Each q As Procs In z.Values
MsgBox(q.Id + " " + q.AppName)
Next



"Robinson" <to******************@myinboxtoomuchtoooften.comwr ote in
message news:eg*******************@news.demon.co.uk...
>
Hi Eric,

You can use For Each on the Values or Keys collection of the hashtable.
Also, I'm not sure if you know about generics, but it's preferable to use
them instead of hashtables where you know the type of object you are
storing. So, for enumeration:
For Each theKey as Integer In MyHashtable.Keys

Next

For Each theValue As MyObject In MyHashtable.Values

Next


For Generics, look up the Dictionary type. You can then make a Dictionary
(of Key, Value ), which is better for type checking at compile time.



Oct 11 '06 #3
You need to create your own enumerator class and add
IEnumerable_GetEnumerator and GetEnumerator methods to your collection
class. See the first code listing on my blog at
http://www.helixoft.com/blog/archives/17 for details.

--
Peter Macej
Helixoft - http://www.helixoft.com
VSdocman - Commenter and generator of class documentation for C#, VB
..NET and ASP .NET code
Oct 11 '06 #4
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

Oct 12 '06 #5
Yes, but how ?
Can you show me some sample code where they have used a hashtable and
then written an enumerator class for it ?

Erick

Peter Macej wrote:
You need to create your own enumerator class and add
IEnumerable_GetEnumerator and GetEnumerator methods to your collection
class. See the first code listing on my blog at
http://www.helixoft.com/blog/archives/17 for details.

--
Peter Macej
Helixoft - http://www.helixoft.com
VSdocman - Commenter and generator of class documentation for C#, VB
.NET and ASP .NET code
Oct 12 '06 #6
Dennis,
your example does not use hashtables. I need to find out how
to do a four each on a custom collection class which stores objects in
side a hastable.

Erick
Dennis wrote:
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
Oct 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
11
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be...
3
by: Locke Nash Cole | last post by:
I've made my first collection. Its a simple one but it works. Now I want to populate a treeview from my collection to display to the user, easy enough... But now comes my dilemma.. say I add a...
1
by: Kyle Novak | last post by:
I have a question about strongly typed objects when looping through a collection based on the CollectionBase object and using a For..Each loop. I have 2 objects: -Invoice: Holds all properties...
7
by: Michael Kellogg | last post by:
I created a custom collection based on System.Collections.Specialized.NameObjectCollectionBase. I also implemented two version of the "Item" property and coded them both as Default properties. ...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
3
by: Matt | last post by:
I am in the process of creating a custom .NET object (this is my first one). I have created a few classes and some of them are collections. When I try to iterate through a collection I receive the...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.