Solved

500pts: Polymorphism help?

Posted on 2006-11-14
7
225 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 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
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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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