Solved

500pts: Polymorphism help?

Posted on 2006-11-14
7
219 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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