Collections and children

Collections and children

Old forum URL: forums.lhotka.net/forums/t/453.aspx


Wal972 posted on Thursday, June 22, 2006

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

DancesWithBamboo replied on Thursday, June 22, 2006

So you have a list of objects that all implement a common interface.  This was done so that you could treat them all the same way in the list. 

But, now you want to call specific methods that are different on each of the concrete classes.  So you want something like ObjectA.CalcTotal and ObjectB.CalcDifference; thus each class actually has different  methods defined.  If this is the case then I believe you need to typecast each of the objects to the subclass that contains the specific method you want to call.  I usually do this in a select-case statement.

Or do you mean thay have specific behavior that goes by the same name?  If this is the case then you should include the prototype of the method you need in your interface and then implement the type specific functionality in your subclasses.  Then you wouldn't need to typecast to your specific types, just the interface.

Wal972 replied on Thursday, June 22, 2006

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

RockfordLhotka replied on Sunday, June 25, 2006

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

 

Wal972 replied on Sunday, June 25, 2006

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

Wal972 replied on Monday, June 26, 2006

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

malloc1024 replied on Monday, June 26, 2006

You dont need to covert or cast anything if total is part of the LineItem interface or abstract class.  Just use the TypeOf() method to check the type of the object in a case statement and then add up the total.  Make sure you use TypeOf() to check against the concrete classes.  If the LineItem interface or abstract class does not contain a total property, use TypeOf() first then cast it to the appropriate class.

Wal972 replied on Thursday, July 06, 2006

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

tetranz replied on Thursday, July 06, 2006

Wal972:
I have been doing some research and it seems a Select Case using Typeof() doesn't work. Any Suggestions ?
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.

Ross

RockfordLhotka replied on Thursday, July 06, 2006

Yeah, as far as I know you can either do that or use the ElseIf syntax (avoiding nested If's)
 
If blah Then
 
ElseIf foo Then
 
ElseIf bar Then
 
ElseIf blob Then
 
End If
 
Basically the same structure as a Select Case really :)
 
Rocky


From: tetranz [mailto:cslanet@lhotka.net]
Sent: Thursday, July 06, 2006 6:38 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Collections and children

Wal972:
I have been doing some research and it seems a Select Case using Typeof() doesn't work. Any Suggestions ?
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.

Ross



RockfordLhotka replied on Monday, June 26, 2006

I am at a client site until June 29, with limited email until then.

Thank you for your understanding,

Rocky

Copyright (c) Marimer LLC