By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

How do i implement inheritance like this?

P: n/a
// 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?

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Nov 17 '05 #2

P: n/a
// 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.

Nov 17 '05 #3

P: n/a
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.

"Bruce Wood" wrote:
// 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.

Nov 17 '05 #4

P: n/a
Amil <Am**@discussions.microsoft.com> wrote:
// 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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.