Solved

500pts: Polymorphism help?

Posted on 2006-11-14
7
215 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:ianinspain
Comment Utility
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
Comment Utility
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
Comment Utility
Did this help you ?
0
 

Author Comment

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

Thanks..

Ian
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

19 Experts available now in Live!

Get 1:1 Help Now