Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

500pts: Polymorphism help?

Posted on 2006-11-14
7
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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