By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Iterating through a Hastable of objects

P: n/a
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


Nov 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

"RSH" <wa*************@yahoo.comwrote in message
news:Oi**************@TK2MSFTNGP02.phx.gbl...
>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


Nov 8 '06 #2

P: n/a
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
"Steve Long" <St**********@NoSpam.comwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
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

"RSH" <wa*************@yahoo.comwrote in message
news:Oi**************@TK2MSFTNGP02.phx.gbl...
>>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



Nov 8 '06 #3

P: n/a
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

"RSH" <wa*************@yahoo.comwrote in message
news:ea**************@TK2MSFTNGP04.phx.gbl...
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
"Steve Long" <St**********@NoSpam.comwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
>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

"RSH" <wa*************@yahoo.comwrote in message
news:Oi**************@TK2MSFTNGP02.phx.gbl...
>>>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




Nov 8 '06 #4

P: n/a
RSH
Pardon my extreme ignornace...

How do i use it?

I think I missed something.

Thanks,
Ron
"Steve Long" <St**********@NoSpam.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
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

"RSH" <wa*************@yahoo.comwrote in message
news:ea**************@TK2MSFTNGP04.phx.gbl...
>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
"Steve Long" <St**********@NoSpam.comwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
>>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

"RSH" <wa*************@yahoo.comwrote in message
news:Oi**************@TK2MSFTNGP02.phx.gbl...
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






Nov 8 '06 #5

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

Steve

"RSH" <wa*************@yahoo.comwrote in message
news:ey*************@TK2MSFTNGP02.phx.gbl...
Pardon my extreme ignornace...

How do i use it?

I think I missed something.

Thanks,
Ron
"Steve Long" <St**********@NoSpam.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
>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

"RSH" <wa*************@yahoo.comwrote in message
news:ea**************@TK2MSFTNGP04.phx.gbl...
>>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
"Steve Long" <St**********@NoSpam.comwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
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

"RSH" <wa*************@yahoo.comwrote in message
news:Oi**************@TK2MSFTNGP02.phx.gbl...
>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
>
>
>
>
>
>




Nov 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.