473,626 Members | 3,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface casting required question

Supposed that a class "MyTestClas s" implement interface "IMyInterfa ce" (not
explicitly).

Now suppose that I make an instantiate of "MyTestClas s" and then use this
instance to pass it to a function with an argument that requires
"IMyInterfa ce" such as:

private void SomeFunction(IM yInterface xyz) {.}

My question is, will this function require a cast to "IMyInterfa ce"? For
example, if I was to do this following, would this require a cast?

MyTestClass experiment = new MyTestClass();
SomeFunction(ex periment); // Cast because argument is type "IMyInterface"? ??

Or does the compiler knows that the instance of the object implement the
interface and it ends up implementing the code the exact same way as if the
"SomeFuncti on" would have the argument of type "MyTestClas s" instead of
"IMyInterfa ce"?

Thank you.
Apr 10 '06 #1
1 1180
Rene <a@b.c> wrote:
Supposed that a class "MyTestClas s" implement interface "IMyInterfa ce" (not
explicitly).

Now suppose that I make an instantiate of "MyTestClas s" and then use this
instance to pass it to a function with an argument that requires
"IMyInterfa ce" such as:

private void SomeFunction(IM yInterface xyz) {.}

My question is, will this function require a cast to "IMyInterfa ce"? For
example, if I was to do this following, would this require a cast?

MyTestClass experiment = new MyTestClass();
SomeFunction(ex periment); // Cast because argument is type "IMyInterface"? ??
No.
Or does the compiler knows that the instance of the object implement the
interface and it ends up implementing the code the exact same way as if the
"SomeFuncti on" would have the argument of type "MyTestClas s" instead of
"IMyInterfa ce"?


There's an implicit conversion from a type which implements an
interface to that interface, so the compiler knows it can call that
method.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #2

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

Similar topics

10
4462
by: The Directive | last post by:
Is there a way in C++ to create a "true" interface. I want to create an absract class that other classes can inherit from and be forced to implement the interface's abstract (pure virtual) methods. However, it has to be a true interface in that it doesn't add any overhead (when the derived object is created) with the interface's constructors and destructor because they don't exist for the interface. Does that make sense? I just want an...
231
23102
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
7
3662
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b; but it complains for the following lines
0
921
by: AStrugglingDeveloper | last post by:
I'm probably missing something horribly obvious but this is the first time I've used the managed extensions for C++ and the documentation seems to indicate that what I am doing should work. I am developing a shim to allow users to develop managed add-ins for an unmanaged product. I have developed a library in MC++ with a public interface that all add-ins must implement. At start up I read a config file to find all the registered...
10
11780
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal Dictionary as IDictionary) If Dictionary is Nothing Then Return Dim o as Object = DirectCast(Dictionary, Object)
2
1615
by: Kevin Frey | last post by:
Consider this simple example: interface IReader { bool Read( ); }; class MyReader : IReader { bool Read( ); // or should it be bool IReader.Read( ) ?
9
1479
by: Sean Chambers | last post by:
Not sure if this is the correct way to go about this, but it seems correct. I'm trying to eliminate dependencies through interfaces and want to minimize casting as much as possible, as a result I have interface inheritance setup like so Here's my class setup: BlogPost : IBlogPost PodcastPost : BlogPost, IPodcastPost
0
2502
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
5
1216
by: Russell Mangel | last post by:
Here is something much closer to what I was trying to do. Thanks to Arne, and Peter and all who helped. Sorry I didn't explain my question better. Russell Mangel Las Vegas, NV // Begin Code using System;
0
8259
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8192
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7188
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6119
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.