471,350 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

OO: Who am I in the hierarchy?

I have an abstract class that is a windows form called "BaseList" that
contains a grid control. This grid control is then populated by any
concrete class that inherits from BaseList. I have a class SpecificList
class that is a kind of BaseList (class SpecificList:BaseList). When a
user interacts with the SpecificList, they will be allowed to save the
layout of that grid. Now, here's the rub.

I want to put the code into BaseList for storing the layout of the grid.
But I need to store the layout uniquely for each instance of BaseList.
In other words if I want to save the layout of SpecificList, the code
executed in BaseList needs to know the name of the concrete class that
instantiated it (SpecificList). Likewise if I had AnotherList:BaseList,
then when a user saves the layout of AnotherList, the code inside of
BaseList needs a way to get the string "AnotherList" out.

My thought is to add an abstract method to BaseList called
"GetConcreteClassName" or something like that. This would then force
all the subclasses of BaseList to implement the method. I believe that
there must be a more elegant solution than that. I would have to
believe that I could use reflection, but I'm not sure if this is a wise
approach or not. Is there a more OO way to figure this out?

Thanks,
Bill Gregg

*** Sent via Developersdex http://www.developersdex.com ***
Feb 14 '06 #1
6 1275
From the description, my understanding is that you have


System.Windows.Forms.Form
^
||
BaseList <>=== Grid
^
||
===============
|| ||
SpecificList AnotherList
BaseList
Overridable Sub LoadData()

SpecificList
Overrides Sub LoadData() - implements loading the grid for the
SpecificList data

AnotherList
Overrides Sub LoadData() - implements loading the grid for the
AnotherList data
Here is where I get a bit fuzzy

You say that want to store the layout uniquely for each 'Instance' of
BaseList but then start to talk about AnotherList.

Do you mean you want it to be unqiue for each concrete class?
Different layouts for SpecificList and AnotherList.

Or,

Do you mean that the user is able to save the layout of which list/grid
they are using irrespective of type?
Also, I'm afraid that I am not clear as to why the baselist code needs
to know the class name - for what are you using it.

Perhaps if you can post some more details we can be of help.
Alan.

Feb 14 '06 #2

Bill Gregg wrote:
I have an abstract class that is a windows form called "BaseList" that
contains a grid control. This grid control is then populated by any
concrete class that inherits from BaseList. I have a class SpecificList
class that is a kind of BaseList (class SpecificList:BaseList). When a
user interacts with the SpecificList, they will be allowed to save the
layout of that grid. Now, here's the rub.

I want to put the code into BaseList for storing the layout of the grid.
But I need to store the layout uniquely for each instance of BaseList.
In other words if I want to save the layout of SpecificList, the code
executed in BaseList needs to know the name of the concrete class that
instantiated it (SpecificList). Likewise if I had AnotherList:BaseList,
then when a user saves the layout of AnotherList, the code inside of
BaseList needs a way to get the string "AnotherList" out.


Why not make StoreLayout a method of BaseList then override it in each
inherited class? The base method can contain such layout-storing code
as is common to all lists, then each subclass' method calls the base
class method then adds its own details.

--
Larry Lard
Replies to group please

Feb 14 '06 #3
Alan,
Your object diagram is spot on, and your first assumption is the
correct one. When a user clicks on "StoreLayout" on the SpecificList
form, I want code in my BaseList class to store the layout in the
registry. These registry entries need to be unique for each kind of
concrete BaseList.

Ideally, I would like to be able to make all of my changes in BaseList
and not need to make a simple change in every concrete class that
derives from BaseList. I am trying to add functionality retroactively
and if I can avoid touching lots and lots of forms, I would like to.

Bill

*** Sent via Developersdex http://www.developersdex.com ***
Feb 14 '06 #4
Larry Lard wrote:

Why not make StoreLayout a method of BaseList then override it in each
inherited class? The base method can contain such layout-storing code
as is common to all lists, then each subclass' method calls the base
class method then adds its own details.

Bill Gregg writes:
The code for storing the layout is generic already. I can have a single
method handle all the additional functionality I need, I just need to
know the name of the concrete class that is inheriting from the BaseList
so that when I store the layout, I can associate the layout with the
form name.
*** Sent via Developersdex http://www.developersdex.com ***
Feb 14 '06 #5

You already have code in the base class for storing the layout and it
is sufficient to store the layout for each/every concrete class, all it
needs is a key/id with which to store/restore the given layout.

The simplest is to use the class name.

public void SaveLayout() {

string layoutID = this.GetType().Name;
Console.WriteLine(String.Format("Saving the layout for {0}.",
layoutID));

}
If we have two derived classes, Derived and AnotherDerived and call
SaveLayout on each

e.g.

Base b;

b = new Derived();
b.SaveLayout();

b = new AnotherDerived();
b.SaveLayout();

then we get

Saving the layout for Derived.
Saving the layout for AnotherDerived.

as the output.
hth,
Alan.

Feb 15 '06 #6

That worked perfectly well Alan. Thanks so much.

Bill Gregg
*** Sent via Developersdex http://www.developersdex.com ***
Feb 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by vcardillo | last post: by
1 post views Thread by Chris Lasher | last post: by
reply views Thread by archway | last post: by
2 posts views Thread by Yaro | last post: by
4 posts views Thread by Dave Veeneman | last post: by
21 posts views Thread by Mark Broadbent | last post: by
2 posts views Thread by Do | last post: by
3 posts views Thread by krzysztof.konopko | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.