Iterating through a Hastable of objects

R

RSH

I have two HashTables (_CompanyDeductions,_CompanyAccruals) that contain
several objects each. I am trying to loop through and print the properties
of the objects in the Hashtables:

I am trying to creat a dynamic function that will print all of the
properties of each of the objects stored in the the HashTable. I can't seem
to get at the properties of the objects.

Thanks for any help!
Ron

This does NOT work:

Public Sub Print(ByVal type As String)

Console.WriteLine(vbCrLf)

Dim al As New Hashtable

Dim o As Object

Console.WriteLine("CompanyId:" & _CompanyID & " CompanyName:" &
_CompanyName)

Select Case Type

Case "Deductions"

al = _CompanyDeductions

Case "Accruals"

al = _CompanyAccruals

End Select

If al.Count > 0 Then

Console.WriteLine(type & " ID" & vbTab & type & " Name" & vbTab & " Rate")

Console.WriteLine("---------------------------------------")

For Each o In al

Console.WriteLine("{0} : {1}", o.value.Name)

Next

Else

Console.WriteLine("No " & type & " exist for this company")

End If

End Sub
 
S

Steve Long

Look at the docs on a Hashtable object. The docs say that the foreach of a
Hastable returns a DictionaryEntry object for each object in the Hashtable.
The Value property of the DictionaryEntry is the object stored at that
location in the HashTable. Therefore it follows that if the Type of the
Value property of each DictionaryEntry is of a particular type, then you
could set a variable of that type equal to the Value property and then
access it's properties.

Dim ca as _CompanyAccruals
Dim cd as _CompanyDeductions

For Each de as DictionaryEntry in myHashTable
if typeof de.Value is _CompanyAccruals then
ca = de.Value
' Now get the properties for ca
else
cd = de.Value
' Now get the properties for cd
end if
Next

What might be a better design is if you created and Interface that had
common properties that both object shared and then Implemented that
Interface in both of those object. Then, you'd only need one variable in the
above code snippet to get at the properties that you want to print out,
given these properties are the implementations that each object shared
Is that too much info???

HTH
Steve
 
R

RSH

Steve,

Thanks for that great reply!

i am having trouble understanding interfaces...would you be able to show me
a snippet of how i might do what you suggested?

Thanks alot!
Ron
 
S

Steve Long

Public Interface MyNewInterface

Property Name() As String

Property ID() As Integer

End Interface

Public Class MyComponentAccruals

Implements MyNewInterface



Public Property ID() As Integer Implements MyNewInterface.ID

Get

End Get

Set(ByVal Value As Integer)

End Set

End Property

Public Property Name() As String Implements MyNewInterface.Name

Get

End Get

Set(ByVal Value As String)

End Set

End Property

' add any other properties/methods you want

End Class

Public Class MyComponentDeductions

Implements MyNewInterface

Public Property ID() As Integer Implements MyNewInterface.ID

Get

End Get

Set(ByVal Value As Integer)

End Set

End Property

Public Property Name() As String Implements MyNewInterface.Name

Get

End Get

Set(ByVal Value As String)

End Set

End Property

' add any other properties/methods you want

End Class
 
R

RSH

Pardon my extreme ignornace...

How do i use it?

I think I missed something.

Thanks,
Ron
 
S

Steve Long

RSH, did you write the two classes that you are trying to get the properties
for in your original post?

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top