Solved

500pts: Polymorphism help?

Posted on 2006-11-14
7
216 Views
Last Modified: 2010-04-16
Hi there,

Can anyone help, i have declared some variables of type ToolWindow which belongs to a custom control in my class like so

private ToolWindow _explorerToolWindow, _userToolWindow, _alarmToolWindow;

All is fine, then under a method i assign these to actual control which actually inherits from ToolWindow hence

                  _explorerToolWindow= new twExplorer(); // this now is a toolwindow but actually is really a twExplorer which derives from toolwindow
                  (_explorerToolWindowas twExplorer).go(); // I have to do this to enable access to .go method.. otherwise its unavailable

but i don't think i am doing it right... Is there a way to assign my _explorerToolWindow directly to a twExplorer... or do i have to use the keyword "as" all the time?

I am a little confused, it is working using this way ... but i think i am going the long way round... there must be a way to automatically change the instance to be of type twExplorer and not toolWindow?

any advice or help really appreciated

thanks in advance

Ian
0
Comment
Question by:ianinspain
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17939829
You are using a reference of type ToolWindow to refer to a subclass of that type.
This effectively masks members of the subtype unless you explicity down-cast to that subtype.

If you want the members of twExplorer to be accessible then declare _explorerToolWindow as a twExplorer instead of as a ToolWindow.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17939845
private twExplorer _explorerToolWindow;
private ToolWindow _userToolWindow, _alarmToolWindow;

instead of

private ToolWindow _explorerToolWindow, _userToolWindow, _alarmToolWindow;
0
 
LVL 16

Assisted Solution

by:muzzy2003
muzzy2003 earned 250 total points
ID: 17939924
What ozy said! Plus, You can check the type of the instance like so if you want to be safe:

if (_explorerToolWindow is twExplorer)
{
    ((_explorerToolWindow)twExplorer).go();
}

If _explorerToolWindow is always going to be a twExplorer, and you want to be able to access methods specific to this type, then you should as ozy says declare it as a twExplorer. This won't, however, prevent you passing it to methods that expect a ToolWindow, though, if that was your concern when you first declare it as a ToolWindow, though. A twExplorer is a ToolWindow, so that'll work.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ianinspain
ID: 17940025
Wow! thanks both.... A little confused now... that fixes my problem i think...

But that was my idea... to use polymorphism .... but it appears its useless.. in this scenerio...

Stupid question but where is polymorphism any good then ... if i can just use twExplorer and still pass to toolwindow??

Thanks agian

ian
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 250 total points
ID: 17940073
It allows code reuse, the fact that twExplorere was able to be derived from ToolWindow.

It also means that you could handle _explorerToolWindow, _userToolWindow, _alarmToolWindow generically as ToolWindows, as in :

ToolWindow[] tools = new ToolWindow[]{explorerToolWindow, _userToolWindow, _alarmToolWindow};
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17947969
Did this help you ?
0
 

Author Comment

by:ianinspain
ID: 17948794
yes thanks... i think i was trying to do a simple thing a complex way :-)

Thanks..

Ian
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now