473,545 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion about interface

According to OO, interface is just a bunch of definitions, no implementation
at all. It's also true when we write our own code in .NET. But I find every
interface provided by .NET has specific implementation.
For example. Control.Invoke Method Executes a delegate on the thread that
owns the control's underlying window handle because it is implemented
ISynchronizeInv oke.Invoke.
Why?

Thanks in advance
Nov 21 '05 #1
4 1203
An interface is just a container for "rules". These rules may include
method, property, event declarations, etc. but do not include any actual
functional code.

Implementing an interface is an agreement to follow the "rules" specified in
the interface. Any class that implements the ISynchronizeInv oke interface,
for example, must also now implement any of the things defined in that
interface (like an Invoke method). It is up to the class that is
implementing the interface to determine what will actually happen when the
Invoke method is called.

A simplified analogy would be that a car's interface includes having a
steering wheel, a gear shifter and the abilities to accelerate and
decelerate. If you were going to build a car, then you'd need to implement
this standard interface (because obviously a car has to have the previously
mentioned things), BUT it would be up to you (the creator of the car) to
determine exactly what kind of steering wheel and gear shifter you wanted
and it would also be up to you to determine exactly HOW you want to
IMPLEMENT the functionality for accelerating and decelerating.

For example:

Public Interface Automobile
Property CurrentGear() As Integer
Sub Accelerate(ByVa l ByHowMuch As Short)
Sub Decelerate(ByVa l ByHowMuch As Short)
End Interface

Public Class Car
Implements Automobile

Public Sub Accelerate(ByVa l ByHowMuch As Short) Implements
Automobile.Acce lerate
'Now, you decide exactly how to make this happen
End Sub

Public Property CurrentGear() As Integer Implements Automobile.Curr entGear
Get
'Now, you decide exactly how to make this happen
End Get
Set(ByVal Value As Integer)
'Now, you decide exactly how to make this happen
End Set
End Property

Public Sub Decelerate(ByVa l ByHowMuch As Short) Implements
Automobile.Dece lerate
'Now, you decide exactly how to make this happen
End Sub
End Class
-Scott


"Rulin Hong" <Ru*******@disc ussions.microso ft.com> wrote in message
news:7E******** *************** ***********@mic rosoft.com...
According to OO, interface is just a bunch of definitions, no
implementation
at all. It's also true when we write our own code in .NET. But I find
every
interface provided by .NET has specific implementation.
For example. Control.Invoke Method Executes a delegate on the thread that
owns the control's underlying window handle because it is implemented
ISynchronizeInv oke.Invoke.
Why?

Thanks in advance

Nov 21 '05 #2
Scott M.
I know what you said.

I give you another example, Postback Event of web server control. Here is
the code.

Namespace CustomControls
Public Class MyButton
Inherits Control
Implements IPostBackEventH andler
' Defines the Click event.
Public Event Click As EventHandler

' Invokes delegates registered with the Click event.
Protected Overridable Sub OnClick(e As EventArgs)
RaiseEvent Click(Me, e)
End Sub

' Method of IPostBackEventH andler that raises change events.
Public Sub RaisePostBackEv ent(eventArgume nt As String) Implements
IPostBackEventH andler.RaisePos tBackEvent
OnClick(EventAr gs.Empty)
End Sub

Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write("< INPUT TYPE=submit name=" & Me.UniqueID & _
" Value='Click Me' />")
End Sub
End Class
End Namespace

In this code, we just simply write
Implements IPostBackEventH andler.RaisePos tBackEvent.
There is none of code about how to postback. In other words, I guess .NET
must have something behind IPostBackEventH andler.RaisePos tBackEvent.

Nov 21 '05 #3

"Rulin Hong" <Ru*******@disc ussions.microso ft.com> wrote in message
news:5C******** *************** ***********@mic rosoft.com...
Scott M.
I know what you said.

I give you another example, Postback Event of web server control. Here is
the code.

Namespace CustomControls
Public Class MyButton
Inherits Control
Implements IPostBackEventH andler
' Defines the Click event.
Public Event Click As EventHandler

' Invokes delegates registered with the Click event.
Protected Overridable Sub OnClick(e As EventArgs)
RaiseEvent Click(Me, e)
End Sub

' Method of IPostBackEventH andler that raises change events.
Public Sub RaisePostBackEv ent(eventArgume nt As String) Implements
IPostBackEventH andler.RaisePos tBackEvent
OnClick(EventAr gs.Empty)
End Sub

Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write("< INPUT TYPE=submit name=" & Me.UniqueID & _
" Value='Click Me' />")
End Sub
End Class
End Namespace

In this code, we just simply write
Implements IPostBackEventH andler.RaisePos tBackEvent.
There is none of code about how to postback. In other words, I guess .NET
must have something behind IPostBackEventH andler.RaisePos tBackEvent.


No. This class "implements " an interface. Therefore, specific
implementation of the interface IS required. The interface definition
itself does NOT contain the code for the methods/properties/et cetera, but
implementing class(es) do...ex:

' The following interface contains no implementation code:
Public Interface IShape
Property Width() As Double

Function CalculateArea() As Double
End Interface

' The following class contains implementation code for the IShape interface
declared above.
Public Class Triangle
Implements IShape

Public Function CalculateArea() As Double _
Implements IShape.Calculat eArea
' Calculate the area of the triangle here.
End Function

Public Property Width() As Double Implements IShape.Width
Get
' Return the width.
End Get
Set(ByVal Value As Double)
' Set the width.
End Set
End Property
End Class

HTH,
Mythran

Nov 21 '05 #4
Not quite. The control itself already knows how to raise a PostBack event,
you are just implementing an interface that defines a handler for that
event. What you do or do not put in that handler does not determine if the
control will raise the event in the first place, only your class's ability
to handle that event.
"Rulin Hong" <Ru*******@disc ussions.microso ft.com> wrote in message
news:5C******** *************** ***********@mic rosoft.com...
Scott M.
I know what you said.

I give you another example, Postback Event of web server control. Here is
the code.

Namespace CustomControls
Public Class MyButton
Inherits Control
Implements IPostBackEventH andler
' Defines the Click event.
Public Event Click As EventHandler

' Invokes delegates registered with the Click event.
Protected Overridable Sub OnClick(e As EventArgs)
RaiseEvent Click(Me, e)
End Sub

' Method of IPostBackEventH andler that raises change events.
Public Sub RaisePostBackEv ent(eventArgume nt As String) Implements
IPostBackEventH andler.RaisePos tBackEvent
OnClick(EventAr gs.Empty)
End Sub

Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write("< INPUT TYPE=submit name=" & Me.UniqueID & _
" Value='Click Me' />")
End Sub
End Class
End Namespace

In this code, we just simply write
Implements IPostBackEventH andler.RaisePos tBackEvent.
There is none of code about how to postback. In other words, I guess .NET
must have something behind IPostBackEventH andler.RaisePos tBackEvent.

Nov 21 '05 #5

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

Similar topics

0
1135
by: serge calderara | last post by:
Dear all, For lonmg time now I try to understand clearly the sue of intervface in .NET by many example and articles,and comments from different source. But seems that each of us has they own ideas of interface outside from the fact of the basic rules of polymorphisme. For me Interface means a kind of FierWall between a class user and...
16
4236
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
6
1929
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used subroutines, etc. Now I'm trying to study this new way and I'm getting some terms confused and can find no clear definition (some even overlap or...
12
1291
by: masoud bayan | last post by:
I've come across something in Interface implementation that I am not sure is correct behavior in VB.NET (and maybe C#) or not? Consider following example: Public Interface IShape
8
1467
by: khalprin | last post by:
Hello, I'm trying to create a component that will be used from .net clients and COM clients. I've got an object model that looks something like this: ISystem IRuntime IConfiguration ICollectionOfConfigurableThings IConfigurableThing
4
2786
by: gabriel | last post by:
Hi Firstly i'd like to thank you for reading this and offer my appreciation for replies in advance. I've recently been writing a program which implements a user-defined API (Robocode to be exact). I, however, need to create my own implementation of an interface in order to perform some arbitary function. My question is, how would i go...
1
1614
by: YellowfinTeam | last post by:
Laws and Myths of Usability & Interface Design 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
17
1805
by: Galian | last post by:
Hi all. I have some questions about interface implementation, if anybody can help me, I will be very thank. So: If i have some interface IA: class IA { virtual void SomePureVirtFunc() = 0; };
5
2386
by: Tony Johansson | last post by:
Hello! Assume you have the following interface and classes shown below. It is said that a class must implement all the methods in the interface it inherits. Below we have class MyDerivedClass that inherits IMyInterface but MyDerivedClass doesn't implement method DoSomething() it inherits it from the base class MyBaseClass. So the...
0
7420
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...
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
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...
1
7446
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...
0
7778
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...
0
6003
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...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
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...

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.