473,480 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Create 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
ISynchronizeInvoke.Invoke.
Why?

Thanks in advance
Nov 21 '05 #1
4 1198
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 ISynchronizeInvoke 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(ByVal ByHowMuch As Short)
Sub Decelerate(ByVal ByHowMuch As Short)
End Interface

Public Class Car
Implements Automobile

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

Public Property CurrentGear() As Integer Implements Automobile.CurrentGear
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(ByVal ByHowMuch As Short) Implements
Automobile.Decelerate
'Now, you decide exactly how to make this happen
End Sub
End Class
-Scott


"Rulin Hong" <Ru*******@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.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
ISynchronizeInvoke.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 IPostBackEventHandler
' 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 IPostBackEventHandler that raises change events.
Public Sub RaisePostBackEvent(eventArgument As String) Implements
IPostBackEventHandler.RaisePostBackEvent
OnClick(EventArgs.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 IPostBackEventHandler.RaisePostBackEvent.
There is none of code about how to postback. In other words, I guess .NET
must have something behind IPostBackEventHandler.RaisePostBackEvent.

Nov 21 '05 #3

"Rulin Hong" <Ru*******@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.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 IPostBackEventHandler
' 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 IPostBackEventHandler that raises change events.
Public Sub RaisePostBackEvent(eventArgument As String) Implements
IPostBackEventHandler.RaisePostBackEvent
OnClick(EventArgs.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 IPostBackEventHandler.RaisePostBackEvent.
There is none of code about how to postback. In other words, I guess .NET
must have something behind IPostBackEventHandler.RaisePostBackEvent.


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.CalculateArea
' 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*******@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.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 IPostBackEventHandler
' 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 IPostBackEventHandler that raises change events.
Public Sub RaisePostBackEvent(eventArgument As String) Implements
IPostBackEventHandler.RaisePostBackEvent
OnClick(EventArgs.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 IPostBackEventHandler.RaisePostBackEvent.
There is none of code about how to postback. In other words, I guess .NET
must have something behind IPostBackEventHandler.RaisePostBackEvent.

Nov 21 '05 #5

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

Similar topics

0
1122
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...
16
4224
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
1918
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...
12
1283
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
1459
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...
4
2771
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...
1
1608
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....
17
1787
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
2374
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...
0
7048
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
6911
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
7050
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,...
0
7091
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...
0
6966
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
4488
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
2999
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
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.