473,388 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How do i implement inheritance like this?

// 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
4 1012
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
// 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance...
14
by: Medi Montaseri | last post by:
Hi, I think my problem is indeed "how to implement something like java's final in C++" The long version.... I have an abstract base class which is inherited by several concrete classes. I...
14
by: alainpoint | last post by:
Hello, I have the need to write the equivalent of Python class methods in C++. Chuck Allison proposes the following (http://www.artima.com/cppsource/simple.html): #include <iostream> using...
22
by: Nemisis | last post by:
Hi everyone, i am creating my own DAL and BLL, and i am not using typed datasets. My problem is that in my DAL i have a Save method whos signiture looks something like: Save(ID as integer,...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
5
by: g18c | last post by:
Hi, im trying to do something and just been caught off guard. I want to implement an interface but only for certain class, but of course when i derive from this base class the derived class also...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
1
by: vimalankvk80 | last post by:
How can we implement third party class, with out using inheritance ? class thirdPartyClass { virtual void func1(); virtual void func2(); }; class sample {
5
by: Tony Johansson | last post by:
Hello! Assume you have the following interface and classes shown below. It is said that a class must implement all the methods in the interface it inherits. Below we have class MyDerivedClass...
1
by: TamusJRoyce | last post by:
After two years of programming relentlessly in Visual Basic .Net, I have never found if there is a way to directly implement all the required properties/functions/subroutines of an interface directly...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.