473,396 Members | 1,797 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,396 software developers and data experts.

Iterating through a Hastable of objects

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
5 1632
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Cliff | last post by:
Can someone tell me ...show me how to I would go about creating a Hastable in a HashTable. Say I have items A B C D. I want to be able to call item A the key, and it will give me back B C and D....
3
by: Ajay | last post by:
I have a global hashtable instance in my application which is accessed by threads within that application for readonly access. Do I need to synchronize the hashtable if readonly access is required?...
34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
2
by: PABLO | last post by:
Hello I have a Hastable, which I need to order it by value and not by key. Regards
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
3
by: Girish | last post by:
How do i populate a static hastable without the need to instantiate the class? any ideas? Im confused. class VAErrorMap { static Hasttable _ErrorMap = new Hashtable(); public VAErrorMap() {...
2
by: MarkAurit | last post by:
Ive been using arraylists in 1.1 to return collections of custom business objects, and thoroughly enjoyed their simple programming style. After hearing of the advantages of generics during a 2.0...
5
by: mikehulluk | last post by:
Ok, Imagine I have a class class C { }; ostream& operator<<(ostream& o, const C& c) { ...}
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.