473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Shortcut for upcasting to call method...

I have an method that takes an object that implements 3 interfaces:

iBase
iBaseExt
iBaseExtVisual

I have a method that takes an object of type iBaseExt but sometimes I am
passing in a iBaseExtVisual. If the object is an iBaseExtVisual then I need
to call a method that is only availble to iBaseExtVisual. Right now I am
checking the type, assigning it to a local iBaseExtVisual and then calling
it. I was wondering if there was a way to cast the original parameter to
the type I want and call the method. I'm sure there is a way I just can't
find the syntax.

Here is my basic code

public void save ( iBaseExt ds)
{
iBaseExtVisual dsv = null;

if (ds is iBaseExtVisual)
{
dsv = (iBaseExtVisual)dsv;
dsv.UniqueMethod();
}

ds.genericMethod();

}

I thought I should be able to do something like

if (ds is iBaseExtVisual) (ds as iBaseExtVisual).UniqueMethod();
That being said... is that 'bad form' either or?

thx

jack
Nov 16 '05 #1
4 1812
Jack Addington <ja********@shaw.ca> wrote:
I have an method that takes an object that implements 3 interfaces:

iBase
iBaseExt
iBaseExtVisual

I have a method that takes an object of type iBaseExt but sometimes I am
passing in a iBaseExtVisual. If the object is an iBaseExtVisual then I need
to call a method that is only availble to iBaseExtVisual. Right now I am
checking the type, assigning it to a local iBaseExtVisual and then calling
it. I was wondering if there was a way to cast the original parameter to
the type I want and call the method. I'm sure there is a way I just can't
find the syntax.

Here is my basic code

public void save ( iBaseExt ds)
{
iBaseExtVisual dsv = null;

if (ds is iBaseExtVisual)
{
dsv = (iBaseExtVisual)dsv;
dsv.UniqueMethod();
}

ds.genericMethod();

}

I thought I should be able to do something like

if (ds is iBaseExtVisual) (ds as iBaseExtVisual).UniqueMethod();
That being said... is that 'bad form' either or?


Well, you end up doing the cast twice, basically. Better would be:

iBaseExtVisual dsv = ds as iBaseExtVisual;
if (dsv != null)
{
dsv.UniqueMethod();
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jack Addington <ja********@shaw.ca> wrote:
I have an method that takes an object that implements 3 interfaces:

iBase
iBaseExt
iBaseExtVisual

I have a method that takes an object of type iBaseExt but sometimes I am
passing in a iBaseExtVisual. If the object is an iBaseExtVisual then I need
to call a method that is only availble to iBaseExtVisual. Right now I am
checking the type, assigning it to a local iBaseExtVisual and then calling
it. I was wondering if there was a way to cast the original parameter to
the type I want and call the method. I'm sure there is a way I just can't
find the syntax.

Here is my basic code

public void save ( iBaseExt ds)
{
iBaseExtVisual dsv = null;

if (ds is iBaseExtVisual)
{
dsv = (iBaseExtVisual)dsv;
dsv.UniqueMethod();
}

ds.genericMethod();

}

I thought I should be able to do something like

if (ds is iBaseExtVisual) (ds as iBaseExtVisual).UniqueMethod();
That being said... is that 'bad form' either or?


Well, you end up doing the cast twice, basically. Better would be:

iBaseExtVisual dsv = ds as iBaseExtVisual;
if (dsv != null)
{
dsv.UniqueMethod();
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Jon,

Thanks for the reply... Am I correct in assuming then that if the type
passed in is not iBaseExtVisual, the dsv object won't be instatiated, thus
null, and an exception won't be fired. Thus the check for null is basically
checking if the cast worked ???

thx

jack

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jack Addington <ja********@shaw.ca> wrote:
I have an method that takes an object that implements 3 interfaces:

iBase
iBaseExt
iBaseExtVisual

I have a method that takes an object of type iBaseExt but sometimes I am
passing in a iBaseExtVisual. If the object is an iBaseExtVisual then I need to call a method that is only availble to iBaseExtVisual. Right now I am checking the type, assigning it to a local iBaseExtVisual and then calling it. I was wondering if there was a way to cast the original parameter to the type I want and call the method. I'm sure there is a way I just can't find the syntax.

Here is my basic code

public void save ( iBaseExt ds)
{
iBaseExtVisual dsv = null;

if (ds is iBaseExtVisual)
{
dsv = (iBaseExtVisual)dsv;
dsv.UniqueMethod();
}

ds.genericMethod();

}

I thought I should be able to do something like

if (ds is iBaseExtVisual) (ds as iBaseExtVisual).UniqueMethod();
That being said... is that 'bad form' either or?


Well, you end up doing the cast twice, basically. Better would be:

iBaseExtVisual dsv = ds as iBaseExtVisual;
if (dsv != null)
{
dsv.UniqueMethod();
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Jack Addington <ja********@shaw.ca> wrote:
Thanks for the reply... Am I correct in assuming then that if the type
passed in is not iBaseExtVisual, the dsv object won't be instatiated, thus
null, and an exception won't be fired. Thus the check for null is basically
checking if the cast worked ???


Yup. It means that you don't have to go through the "is" as well as the
cast - it speeds things up.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
Lets suppose I have an class called DatastoreSelect. It has a method called getNextRow(). This method is not abstract, it is fully implemented. It also has a method called setInfoToBeSought, which...
2
by: Judith | last post by:
Hi there everyone I've got: class A { ... }; class B : public A { ... }
0
by: Marcel | last post by:
Hi, the application I am writing has a MainMenu and a DataGrid (UltraGrid). Now I have to edit the text of each cell in this grid. So far so good. All works fine in editmode but if I press a...
0
by: Jack Addington | last post by:
I have an method that takes an object that implements 3 interfaces: iBase iBaseExt iBaseExtVisual I have a method that takes an object of type iBaseExt but sometimes I am passing in a...
5
by: Bob Trabucco | last post by:
Does anyone know how you can do an upcase in VB.NET? Example: Public Class Class1 Private s As String = "Hello World" Protected Property Prop1() As String Get Return s End Get
4
by: Amod | last post by:
I want to call a child class constructor using the base class instance ... whats the xact mechanism to perform this function ? Regards, Amod
10
by: Michael Maes | last post by:
Hi, I want to have a Button.Click event 'raise' a shortcut. If eg Button1 is Clicked, I want to have a shortcut 'CTRL + F12' activated. What's the best way to acchieve this. TIA, Michael
5
by: Mark | last post by:
I can't seem to get this subclass to upcast to a baseclass... see comments below #include <cstdlib> #include <iostream> #include "Base.h" #include "Sub1.h" using namespace std;
0
by: Juha Nieminen | last post by:
If I'm not mistaken, general OOP wisdom says that upcasting should usually be avoided if possible. I have a situation, however, where I can't think of a better way than upcasting for this specific...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.