473,749 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface Rewrite?

How to get the function from "rewrited interface implement"?

interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : I {
int I.Value { get { return 2; } }
}

....
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
v = ((I)a).Value; // v = 2 ???
....

In B, I implemented 2 times. first in A, then B.
The instance of B alway return I.Value from B implement(2)!

When break in debug mode VS show the b has
base class A, and the interface implement value
is 1(I implement in A = 1) not 2(I implment in B = 2).
It seams i can get the base rewrited interface implement,
But i donot know how.

Someone can give me some tip?

greenxiar

Oct 14 '07 #1
6 1447
greenxiar wrote:
How to get the function from "rewrited interface implement"?

interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : I {
int I.Value { get { return 2; } }
}

...
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
You can't do that. The "a" variable is a referece to an instance of the
A class, and it can't hold a reference to an instance of the B class, as
the B class doesn't inherit from the A class.
v = ((I)a).Value; // v = 2 ???
...

In B, I implemented 2 times. first in A, then B.
The instance of B alway return I.Value from B implement(2)!

When break in debug mode VS show the b has
base class A, and the interface implement value
is 1(I implement in A = 1) not 2(I implment in B = 2).
It seams i can get the base rewrited interface implement,
But i donot know how.
It's always the method in the actual class that will be called. You
can't change the behaviour of an object by just changing the type of the
reference.

--
Göran Andersson
_____
http://www.guffa.com
Oct 14 '07 #2
The interface implement cannot "override" is a problem. When class function
override or rewrite,
it can still call "base." of the low level function. Interface rewrite hide
all the low level implement.
That means interface is not overridable, the new implement can never call
previous implement function.

But it is strange. The VS Debug can reveal the low level interface detail!
It seams not a formal way.
I donot know how the VS do it. Does the dotnet framework has some mistery
thing of interface issure?

"Göran Andersson" <gu***@guffa.co m????
news:Oy******** ******@TK2MSFTN GP02.phx.gbl...
greenxiar wrote:
>How to get the function from "rewrited interface implement"?

interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : I {
int I.Value { get { return 2; } }
}

...
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;

You can't do that. The "a" variable is a referece to an instance of the A
class, and it can't hold a reference to an instance of the B class, as the
B class doesn't inherit from the A class.
> v = ((I)a).Value; // v = 2 ???
...

In B, I implemented 2 times. first in A, then B.
The instance of B alway return I.Value from B implement(2)!

When break in debug mode VS show the b has
base class A, and the interface implement value
is 1(I implement in A = 1) not 2(I implment in B = 2).
It seams i can get the base rewrited interface implement,
But i donot know how.

It's always the method in the actual class that will be called. You can't
change the behaviour of an object by just changing the type of the
reference.

--
Göran Andersson
_____
http://www.guffa.com

Oct 14 '07 #3
greenxiar wrote:
The interface implement cannot "override" is a problem. When class function
override or rewrite,
it can still call "base." of the low level function. Interface rewrite hide
all the low level implement.
That means interface is not overridable, the new implement can never call
previous implement function.
There is no previous implementation when it comes to interfaces. The
interface is just a contract, and the class that implements it just
fulfills the contract. The class may have a base class, but that has
nothing to do with the interface.
But it is strange. The VS Debug can reveal the low level interface detail!
It seams not a formal way.
I donot know how the VS do it. Does the dotnet framework has some mistery
thing of interface issure?
There is nothing mysterious. Visual Studio just uses reflection to show
the members of the actual object. It does that for any reference, and it
has nothing to do with the interface.
"Göran Andersson" <gu***@guffa.co m????
news:Oy******** ******@TK2MSFTN GP02.phx.gbl...
>greenxiar wrote:
>>How to get the function from "rewrited interface implement"?

interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : I {
int I.Value { get { return 2; } }
}

...
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
You can't do that. The "a" variable is a referece to an instance of the A
class, and it can't hold a reference to an instance of the B class, as the
B class doesn't inherit from the A class.
>> v = ((I)a).Value; // v = 2 ???
...

In B, I implemented 2 times. first in A, then B.
The instance of B alway return I.Value from B implement(2)!

When break in debug mode VS show the b has
base class A, and the interface implement value
is 1(I implement in A = 1) not 2(I implment in B = 2).
It seams i can get the base rewrited interface implement,
But i donot know how.
It's always the method in the actual class that will be called. You can't
change the behaviour of an object by just changing the type of the
reference.

--
Göran Andersson
_____
http://www.guffa.com


--
Göran Andersson
_____
http://www.guffa.com
Oct 14 '07 #4
Sorry, the code have some mistake.

using System;

namespace InterfaceOverri de {
interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : A, I {
int I.Value { get { return 2; } }
}

class Program {
static void Main(string[] args) {
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
v = ((I)a).Value; // v = 2 ???

}
}
}

My Question is how to query the I.Value = 1 from instance of B class.

"Göran Andersson" <gu***@guffa.co m????
news:e%******** ********@TK2MSF TNGP02.phx.gbl. ..
greenxiar wrote:
>The interface implement cannot "override" is a problem. When class
function override or rewrite,
it can still call "base." of the low level function. Interface rewrite
hide all the low level implement.
That means interface is not overridable, the new implement can never call
previous implement function.

There is no previous implementation when it comes to interfaces. The
interface is just a contract, and the class that implements it just
fulfills the contract. The class may have a base class, but that has
nothing to do with the interface.
>But it is strange. The VS Debug can reveal the low level interface
detail! It seams not a formal way.
I donot know how the VS do it. Does the dotnet framework has some mistery
thing of interface issure?

There is nothing mysterious. Visual Studio just uses reflection to show
the members of the actual object. It does that for any reference, and it
has nothing to do with the interface.
>"Göran Andersson" <gu***@guffa.co m????
news:Oy******* *******@TK2MSFT NGP02.phx.gbl.. .
>>greenxiar wrote:
How to get the function from "rewrited interface implement"?

interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : I {
int I.Value { get { return 2; } }
}

...
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
You can't do that. The "a" variable is a referece to an instance of the
A class, and it can't hold a reference to an instance of the B class, as
the B class doesn't inherit from the A class.

v = ((I)a).Value; // v = 2 ???
...

In B, I implemented 2 times. first in A, then B.
The instance of B alway return I.Value from B implement(2)!

When break in debug mode VS show the b has
base class A, and the interface implement value
is 1(I implement in A = 1) not 2(I implment in B = 2).
It seams i can get the base rewrited interface implement,
But i donot know how.
It's always the method in the actual class that will be called. You
can't change the behaviour of an object by just changing the type of the
reference.

--
Göran Andersson
_____
http://www.guffa.com



--
Göran Andersson
_____
http://www.guffa.com

Oct 14 '07 #5
greenxiar wrote:
Sorry, the code have some mistake.

using System;

namespace InterfaceOverri de {
interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : A, I {
int I.Value { get { return 2; } }
}

class Program {
static void Main(string[] args) {
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
v = ((I)a).Value; // v = 2 ???

}
}
}

My Question is how to query the I.Value = 1 from instance of B class.
You can't.

As you have implemented the interface explicity, you can't use the Value
property from a reference to a class instance, only from a reference to
the interface.

When you cast the reference to an interface reference, it doesn't matter
if it was a reference to an instance of the A class or the B class
before you casted it.

--
Göran Andersson
_____
http://www.guffa.com
Oct 14 '07 #6
If you just break and debug, VS shows detail of every hidden interfaces.
You can see I.Value = 1(base A) from B class. It is strange!!!
I think there is another way to get the hidden interface implementation.
Is it a hidden secrete of .NET?

"Göran Andersson" <gu***@guffa.co m????
news:OY******** ******@TK2MSFTN GP06.phx.gbl...
greenxiar wrote:
>Sorry, the code have some mistake.

using System;

namespace InterfaceOverri de {
interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : A, I {
int I.Value { get { return 2; } }
}

class Program {
static void Main(string[] args) {
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
v = ((I)a).Value; // v = 2 ???

}
}
}

My Question is how to query the I.Value = 1 from instance of B class.

You can't.

As you have implemented the interface explicity, you can't use the Value
property from a reference to a class instance, only from a reference to
the interface.

When you cast the reference to an interface reference, it doesn't matter
if it was a reference to an instance of the A class or the B class before
you casted it.

--
Göran Andersson
_____
http://www.guffa.com

Oct 15 '07 #7

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

Similar topics

4
8203
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by making my contnent dlls implement an interface created in vb6. The viewer application is bound to this interface. This way, I am able to add Content without redeploying the dlls (I just have to add the new dlls). I want to write new content for...
6
6163
by: shailesh kumar | last post by:
Hi, I need to design data interfaces for accessing files of very large sizes efficiently. The data will be accessed in chunks of fixed size ... My data interface should be able to do a random seek in the file, as well as sequential access block by block.... One aspect of the usage of this interface is that there is quite good chance of accessing same blocks again and again by the application..
4
1407
by: Jeff Molby | last post by:
First off, this is a VB application, but I know you C# are used to working at the lower levels like this, so I'm hoping this is an easy one for you. Then you can continue to feel superior to us VB-types <G> BTW, I am somewhat proficient in C#, but my bosses and coworkers barely know VB, so no, I can't just rewrite it in C# <g> Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your...
5
4656
by: cs | last post by:
We have a VB.NET interface that I just dont have the time to rewrite at the moment, and I need to implement it on C#, I have done it already however I get the following error when referencing my implementation, I have added all the references needed already. c:\inetpub\wwwroot\webclient_1\WebClasses.vb(44): Reference required to assembly 'WebInterfaces' containing the implemented interface 'WebLink_Interface'. Add one to your project.
26
2102
by: Marius Horak | last post by:
As in subject. Thanks MH
3
330
by: John Underwood | last post by:
Hi.. I was looking at interface, and I have a example in the docs i'll paste below.. I'm not grasping what you would gain by using a interface, does any one have a brief description of their benefit? Thanks, John Underwood
3
1276
by: Rob Perkins | last post by:
Hi, Is there anything out there which can take either a COM interface definition as an input, or a VB6 class file as an input, and return a ..NET Interface definition block as an output? I need to refactor a block of VB6 code, and the converters built into VS 2005 aren't working at all for me. Rob
2
979
by: Thomas Schoch | last post by:
I have written an interface IAddin for addins for my application. Now I have to add a new property to this addin. What is the best way to do this? I don't want to rewrite or recompile the existing addins because they are already installded on some client machines. My idea: Create a new interface IAddin2 which inherits from IAddin and add the new property. In the host application I can test wheter an addin support interface IAddin or...
3
1689
by: hufaunder | last post by:
I have an interface ITest that includes an event TestStatusChange. There is also a class Test that implements ITest. In one of the functions of Test I want to call the event (see code at the end) but get the following error: "The event 'eventTest.Test.TestStatusChanged' can only appear on the left hand side of += or -=. All samples I saw seem to do the same I am doing. What am I missing?
0
8996
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
8832
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
9388
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
9333
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
9254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6800
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
6078
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.