473,508 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance Question

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 Fallon


Nov 20 '05 #1
7 1252
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:u7**************@TK2MSFTNGP12.phx.gbl...
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 Fallon

Nov 20 '05 #2
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 Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:u7**************@TK2MSFTNGP12.phx.gbl...
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 Fallon


Nov 20 '05 #3
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... 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 Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:u7**************@TK2MSFTNGP12.phx.gbl...
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 Fallon



Nov 20 '05 #4
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(Ch r(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).Tex t

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).Tex t & " DESC"

'add down icon

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

Else

mSortDirection = "ASC"

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

'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 Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:eW**************@tk2msftngp13.phx.gbl...
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:u7**************@TK2MSFTNGP12.phx.gbl...
> 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 Fallon
>
>
>
>



Nov 20 '05 #5
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ew**************@TK2MSFTNGP11.phx.gbl...
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(Ch r(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).Tex t

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).Tex t & " DESC"

'add down icon

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

Else

mSortDirection = "ASC"

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

'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 Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:eW**************@tk2msftngp13.phx.gbl...
> 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
>
>
> "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> news:u7**************@TK2MSFTNGP12.phx.gbl...
> > 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 Fallon
> >
> >
> >
> >
>
>



Nov 20 '05 #6
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 Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ew**************@TK2MSFTNGP11.phx.gbl...
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(Ch r(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).Tex t

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).Tex t & " DESC"

'add down icon

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

Else

mSortDirection = "ASC"

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

'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 Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> 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 Fallon
>
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in

message
> news:eW**************@tk2msftngp13.phx.gbl...
> > 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
> >
> >
> > "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> > news:u7**************@TK2MSFTNGP12.phx.gbl...
> > > 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 Fallon
> > >
> > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #7
Joe and Jay,

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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ew**************@TK2MSFTNGP11.phx.gbl...
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(Ch r(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).Tex t

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).Tex t & " DESC"

'add down icon

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

Else

mSortDirection = "ASC"

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

'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 Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:eW**************@tk2msftngp13.phx.gbl...
> 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
>
>
> "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> news:u7**************@TK2MSFTNGP12.phx.gbl...
> > 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 Fallon
> >
> >
> >
> >
>
>



Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3723
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
2
2173
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can...
4
12799
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
8
7816
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
22
23319
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
6309
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
2084
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
5
2448
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
1807
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
8
328
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to...
0
7229
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
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7398
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
5637
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,...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.