|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Iterating through a Hastable of objectsseveral 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 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 Show quoteHide quote "RSH" <way_beyond_o***@yahoo.com> wrote in message news:OigTyh2AHHA.4292@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 > > > > > > 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 Show quoteHide quote "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message news:uLqkBc3AHHA.2328@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" <way_beyond_o***@yahoo.com> wrote in message > news:OigTyh2AHHA.4292@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 >> >> >> >> >> >> > > 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 Show quoteHide quote "RSH" <way_beyond_o***@yahoo.com> wrote in message news:eaqS7h3AHHA.4892@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" <Steve_Noneya@NoSpam.com> wrote in message > news:uLqkBc3AHHA.2328@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" <way_beyond_o***@yahoo.com> wrote in message >> news:OigTyh2AHHA.4292@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 >>> >>> >>> >>> >>> >>> >> >> > > Pardon my extreme ignornace...
How do i use it? I think I missed something. Thanks, Ron Show quoteHide quote "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message news:egBUSm3AHHA.3540@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" <way_beyond_o***@yahoo.com> wrote in message > news:eaqS7h3AHHA.4892@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" <Steve_Noneya@NoSpam.com> wrote in message >> news:uLqkBc3AHHA.2328@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" <way_beyond_o***@yahoo.com> wrote in message >>> news:OigTyh2AHHA.4292@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 >>>> >>>> >>>> >>>> >>>> >>>> >>> >>> >> >> > > RSH, did you write the two classes that you are trying to get the properties
for in your original post? Steve Show quoteHide quote "RSH" <way_beyond_o***@yahoo.com> wrote in message news:eysPmr3AHHA.996@TK2MSFTNGP02.phx.gbl... > Pardon my extreme ignornace... > > How do i use it? > > I think I missed something. > > Thanks, > Ron > > > "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message > news:egBUSm3AHHA.3540@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" <way_beyond_o***@yahoo.com> wrote in message >> news:eaqS7h3AHHA.4892@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" <Steve_Noneya@NoSpam.com> wrote in message >>> news:uLqkBc3AHHA.2328@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" <way_beyond_o***@yahoo.com> wrote in message >>>> news:OigTyh2AHHA.4292@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 >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>> >>>> >>> >>> >> >> > >
Learning OOP conceptual question
What do you call nested If...Then search loops? SortedList - bug or undocumented behavior ? Idle time Problem Adding and Using Resource Files File Aging Program convert idl to c# or tlb code collapsing in vb.net 2005 Find a control on a form of a specific name VB.NET Delegates |
|||||||||||||||||||||||