I had an earlier post on collections and their children I am still confused by a couple of things so help would be appreciated.
Situation, I have a collection of line items in a payroll pay, similiar to line items in invoices. The different types of line items have specific behavior and rules. No problems with that. If I need to refer to particular types ie. my SalaryLineItem or my HourlyLineItem or find out what type they are so that I can make certain calculations.
How do I specific the type if I used a common interface for the them of lineitems. Do I need to create an abstract base class ?
Hope this makes sense
Elizabeth
Sort of.
I need particular items for various calculations. For example if I am working out the gross pay then I need items of the class SalaryLineItem or HourlyLineItem. Deductions are the same etc. Also I need to be able to get the specific setting of some to determine if their pre or post tax deductions, whether the SalaryLineItem or HourlyLineItem are taxable or not.
So I'm not sure of how to process. I can't seem to check if they are of a particular class if I set up the call as follows
For Each ILineItem in LineItemsCollection
Next
because it only shows the stuff common to the ILineItem interface. So how do I reference itstead.
Help
The following is one of several possible approaches. You could also use the TypeOf operator to check for various types. The following is likely the most performant option, but you can get more readable code (imo) using TypeOf with If..ElseIf statements.
For Each item As ILineItem In LineItemsCollection
' do interface stuff with item as you wish
Dim item1 As LineItem1 = TryCast(item, LineItem1)
If item1 IsNot Nothing Then
' do LineItem1 specific stuff here
Else
Dim item2 As LineItem2 = TryCast(item, LineItem2)
If item2 IsNot Nothing Then
' do LineItem2 specific stuff here
End If
End If
Next
Will this still work if the New() is private. I mean can I declare as
Dim item1 as LineItem1.NewLineItem = TryCast(item, LineItem1)
And how do I get the class type ie. SalaryLineitem in this situation as well ?
Thanks
OK one last question (i hope)
if I want to get totals based on different types for example the salaryline items or deductions ? do I need to convert in order to get the specific information as well as the type.
Thanks
Elizabeth
I have been doing some research and it seems a Select Case using Typeof() doesn't work. Any Suggestions ?
I need to Select from a group of types so I woudl prefer to use a Select Case if I can rather than nested If Statements
Thanks
If there is really no other way then you could always use GetType().Name to get to a string and then do a Select Case to test for string values. Of course its not strongly typed then and therefore prone to typos.Wal972:I have been doing some research and it seems a Select Case using Typeof() doesn't work. Any Suggestions ?
From: tetranz [mailto:cslanet@lhotka.net]
Sent: Thursday, July 06, 2006 6:38 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Collections and childrenIf there is really no other way then you could always use GetType().Name to get to a string and then do a Select Case to test for string values. Of course its not strongly typed then and therefore prone to typos.Wal972:I have been doing some research and it seems a Select Case using Typeof() doesn't work. Any Suggestions ?
Ross
Copyright (c) Marimer LLC