OO: Who am I in the hierarchy?

  • Thread starter Thread starter Bill Gregg
  • Start date Start date
B

Bill Gregg

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
 
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.
 
Bill said:
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.
 
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
 
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.
 
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.
 
Back
Top