Inheritance Question

  • Thread starter Thread starter Joe Fallon
  • Start date Start date
J

Joe Fallon

I have a WinForm that is a Base class. It has many controls on it and a lot
of generic code behind.
When I inherit the form I override 5 methods and now each child form has the
same look and feel and functionality. This part all works fine.

(I learned this morning that if you override a method that also has an Event
Handler then you should NOT include the event handler a 2nd time. I had a
devil of a time figuring out why a block of code was running twice!)

My question concerns one of my child methods.
It is very generic except for one thing - each child form declares a Private
variable named "list" which is a strongly typed collection of business
objects. This variable is used in the child procedure.

Is there any way to "move" this generic procedure to the Base class and yet
still know which strongly typed collection to use? When I try to copy the
code to the Base class VS underlines the "list" variable - which I would
expect since the Base form does not see the variable declared in the child.

Thanks for clarifying this for me.
 
Joe,
I would recommend a Template Method Pattern.

The generic routine would be in the base class, when ever it needed the
"list" it would use an overridable List Property, that each derived class
would override to return the strongly typed list. Of course this means that
each strongly typed collection needs to have a common interface or base
class...

Public Class Base

Protected Overridable Readonly Property List As ICollection

Public Sub Generic()
For each item as object in List
' do something interesting with the item.
Next
End Sub

End Class

Public Class Derived : Inherits Base

Private m_list As StronglyTypedCollection

Protected Overrides Readonly Property List As ICollection
Get
Return m_list
End Get
End Sub

End Class

You could make List public if it makes sense to expose the collection
publicly from Base, normally I use the Template Method Pattern as an
implementation detail, so its Protected. Instead of a overridable property,
you could define it as a overridable Sub or Function.

Hope this helps
Jay
 
Jay,
Thanks for responding. I am having trouble getting it working so here are
some more details:
Ican get it to work if I specify the type of collection but that defeats the
purpose!

The Base form has this:
AcctROC is a ReadOnlyCollection of CSLA business objects.
All ROCs inherit from ReadOnlyCollectionBase
So I can have a CostCenterROC or a VendorROC, etc.

Protected Overridable ReadOnly Property List() As AcctROC
Get

'override this

End Get

End Property

Protected Overridable Sub

Protected Overridable Sub DoSomething
'use the sort method specific to each ROC. (This is NOT a method in
ReadOnlyCollectionBase.)
List.Sort
End Sub

In the Acct child form:
Private mlist As AcctROC

Protected Overrides ReadOnly Property List() As AcctROC

Get

Return mlist

End Get

End Property

The problem is I have declared the type of collection too specifically.
But if I use ReadOnlyCollectionBase or ICollection I can't get back the
correct type of object (AcctROC) in order to use its methods.

FYI - I am just trying to learn this pattern. I can always just go back to
using the method in each child class.

Thanks for helping!
 
Joe,
The problem is I have declared the type of collection too specifically.
But if I use ReadOnlyCollectionBase or ICollection I can't get back the
correct type of object (AcctROC) in order to use its methods.

Remember in the base class you need to treat your collection generically,
you should not need to get to specific AcctROC methods or even care that you
specifically have a AcctROC object.

Where specifically are the you attempting to use methods of a AcctROC
object? in the AcctForm or in the base form?

If I'm reading you correctly you have
AcctForm which has a AcctROC
CostCenterForm which has a CostCenterROC
VendorForm which has a VendorROC

Just as each of your forms inherit from your Base Form which has generic
code, you may need to introduce a Base collection that is sandwiched between
AcctROC & ReadOnlyCollection that has generic methods. In other words, both
the Form & the Collection may need template methods to get this to work...

Can you post the "generic" routine you are dealing with, with an explanation
of how it is different in each specific form?

Hope this helps
Jay
 
Jay,
"Remember in the base class you need to treat your collection generically,
you should not need to get to specific AcctROC methods or even care that you
specifically have a AcctROC object."

I think this is the key. I did post the common method for each ROC:
list.Sort

Because I DO need to have a specific type of ROC in the base class I am
violating the quote above.
This tells me that my first idea was the correct one and that each child
class needs to implement its own method because of the need for strong
typing.

I think I have learned something from this discussion and have clarified
some ideas.
I really didn't think there was a way to do what I as trying to do, but I
wanted to confirm it.

Thanks.

FYI - here is the full routine inside each child form but note that
list.Sort is a specific instance of a ROC like AcctROC when in Acct form and
VendorROC when in vendor form.
(Also note that I used one of YOUR old suggestions about how to add sort
icons to a list view!)

My original goal:
If it wasn't for the List variable I could have all this code in the Base
class instead of copying to each child form.

Protected Overridable Sub dvDisplay_ColumnClick(ByVal sender As Object,
ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles
dvDisplay.ColumnClick
Try

'remove sort icon from old column.

If SortColumn <> -1 Then

dvDisplay.Columns.Item(SortColumn).Text =
dvDisplay.Columns.Item(SortColumn).Text.TrimEnd(Chr(32), Chr(32), Chr
(32), ChrW(&H25B2), ChrW(&H25BC))

End If

If e.Column <> SortColumn Then

'do the sort

SortColumn = e.Column

List.Sort = CType(sender,
DataListView.DataListView).Columns(SortColumn).Text

mSortDirection = "ASC"

'add sort icon to new column.

dvDisplay.Columns.Item(SortColumn).Text &= " " & ChrW(&H25B2)

Else

If mSortDirection = "ASC" Then

mSortDirection = "DESC"

list.Sort = CType(sender,
DataListView.DataListView).Columns(SortColumn).Text & " DESC"

'add down icon

dvDisplay.Columns.Item(SortColumn).Text &= " " & ChrW(&H25BC)

Else

mSortDirection = "ASC"

list.Sort = CType(sender,
DataListView.DataListView).Columns(SortColumn).Text

'add up icon

dvDisplay.Columns.Item(SortColumn).Text &= " " & ChrW(&H25B2)

End If

End If

dvDisplay.DataSource = list

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub
 
Joe,
I haven't given up on this question, its been a busy week, and I haven't had
the time to look at it yet, I should have time this weekend...

Jay
 
Jay,
Thanks.

I thought you might have concluded I have put this to bed.
(I sort of did - but I would be interested in learning if there really is
some neat trick that could resolve this.)
 
Joe and Jay,

Do you know of a sample in the SDK or on the net demonstrating the
techiniques you are talking about here?
 
Back
Top