How do i implement inheritance like this?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

// there are 2 forms inheriting from EditorTemplate
// ItemsEditor and CategoriesEditor
public EditorTemplate CreateForm(string inheritedFormName)
{
// EditorTemplate editor; // makes editor in scope, but not instanstiated.
if (inheritedFormName == "Categories")
CategoriesEditor editor = new CategoriesEditor();
else if (inheritedFormName == "Items")
ItemsEditor editor = new ItemsEditor();
return editor; // IDE says this is not defined within scope.
}
How do I cast up? to the inheriting forms?
 
I'm not 100% sure about this, and I'm not near an IDE to test it, but
take off the declaration BEFORE the editor variable name, under the
conditionals.

public EditorTemplate CreateForm(string inheritedFormName)
{
// EditorTemplate editor; // makes editor in scope, but not
instanstiated.
if (inheritedFormName == "Categories")
editor = new CategoriesEditor();
else if (inheritedFormName == "Items")
editor = new ItemsEditor();
return editor;
}

Try that and see if it works.
 
// there are 2 forms inheriting from EditorTemplate
// ItemsEditor and CategoriesEditor
public EditorTemplate CreateForm(string inheritedFormName)
{
// EditorTemplate editor; // makes editor in scope, but not
instanstiated.
if (inheritedFormName == "Categories")
editor = new CategoriesEditor();
else if (inheritedFormName == "Items")
editor = new ItemsEditor();
return editor; // IDE says this is not defined within scope.
}

You declare "editor" only once... as an EditorTemplate. Then you assign
either a CategoriesEditor object or an ItemsEditor object to it. Later
on, if you want to know which one it is, you can:

CategoriesEditor catEdit = editor as CategoriesEditor;
if (catEdit != null)
...

or

if (editor is CategoriesEditor)
...

or

CategoriesEditor catEditor = (CategoriesEditor)editor;

The last form throws an exception if editor is not a CategoriesEditor.
 
I'm a newbie in C# but this seems to me a lot of work to use a generic Click
event method to handle my menu items, which is what I am trying to do. I
don't like that my class code is littered with individual click methods for
similar menu options. Although, I think that what you suggest will work for
me, I may have to do the individual click methods for now. Thanks.
 
Amil said:
// there are 2 forms inheriting from EditorTemplate
// ItemsEditor and CategoriesEditor
public EditorTemplate CreateForm(string inheritedFormName)
{
// EditorTemplate editor; // makes editor in scope, but not instanstiated.
if (inheritedFormName == "Categories")
CategoriesEditor editor = new CategoriesEditor();
else if (inheritedFormName == "Items")
ItemsEditor editor = new ItemsEditor();
return editor; // IDE says this is not defined within scope.
}
How do I cast up? to the inheriting forms?

You should uncomment the first line, but make sure you do something
appropriate if the inherited form name is neither "Categories" nor
"Items". What do you want to happen in that case?
 
Back
Top