473,503 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can a method implement an interface and handle an event at the same time?

hi all,

lets say i have a usercontrol which implements a custom interface. like
so:

-------------------------------------------------

interface IMyInterface
sub buttonClick()
end interface

class MyParentControl
protected MyButton as Button
end class

class MyUserControl
inherits MyParentControl
implements IMyInterface

sub buttonClick() implements IMyInterface.buttonClick
end sub
end class

-------------------------------------------------

here's the catch. i want MyUserControl.buttonClick to also handle
MyButton.Click(), and i want the code that wires up that event to be in
MyParentControl, not MyUserControl.

is it possible? when i try to put "AddHandler MyButton.Click, AddressOf
Me.buttonClick" in MyParentControl, the compiler tells me that
buttonClick is not a member of MyParentControl (obviously)

not sure how i should move forward here. putting the AddHandler call
into MyUserControl is something i'd like to avoid.

thanks!

Sep 14 '06 #1
6 2434
>is it possible?
You can do it like this

MustInherit class MyParentControl
protected WithEvents MyButton as Button

Protected MustOverride Sub buttonClick() Handles MyButton.Click
end class

class MyUserControl
inherits MyParentControl
implements IMyInterface

Protected Overrides sub buttonClick() implements
IMyInterface.buttonClick
end sub
end class
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 15 '06 #2
true, but VS 2005 still cannot open abstract classes in design view,
which is one of the primary features of the code i am trying to write.
(the old code is abstract and i'm purposefully changing from that.)

i'm just trying to find out if there is a clean way to mimic the
abstract method definitions while avoiding abstract classes. i do
realize that i can't force any classes which inherit from
MyParentControl to also implement IMyInterface, but since interfaces
are the only other way i know of which enforce certain methods to be
defined that is the route i am trying to take.

but thank you for the suggestion

-keith

Mattias Sjögren wrote:
is it possible?

You can do it like this

MustInherit class MyParentControl
protected WithEvents MyButton as Button

Protected MustOverride Sub buttonClick() Handles MyButton.Click
end class

class MyUserControl
inherits MyParentControl
implements IMyInterface

Protected Overrides sub buttonClick() implements
IMyInterface.buttonClick
end sub
end class
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 15 '06 #3
ke*************@gmail.com wrote:
interface IMyInterface
sub buttonClick()
end interface

class MyParentControl
protected MyButton as Button
end class

class MyUserControl
inherits MyParentControl
implements IMyInterface

sub buttonClick() implements IMyInterface.buttonClick
end sub
end class

-------------------------------------------------

here's the catch. i want MyUserControl.buttonClick to also handle
MyButton.Click(), and i want the code that wires up that event to be in
MyParentControl, not MyUserControl.

is it possible?
The biggest problem here is that event handlets have a distinctive
signature - "name( Object, EventArgs )" - that your Interface method
doesn't have. You're going to have to have a /separate/ routine that
does the actual work and call it from (a) the button's "click" handler
and (b) the method that implements the Interface.

Class MyParentControl

Protected WithEvents MyButton as Button

Sub New()
MyButton = New Button
With MyButton
...
End With
End Sub

Protected Sub buttonClick()
' code for dealing with "Click" goes here
End Sub

Private Sub MyButton_Click(sender As Object, e As EventArgs) _
Handles MyButton.Click

Me.buttonClick()

End Sub

End Class
Class MyUserControl
Inherits MyParentControl
Implements IMyInterface

Sub New()
MyBase.New()

End Sub

Private Sub Something() _
Implements IMyInterface.buttonClick

MyBase.buttonClick()

End Sub

End Class

HTH,
Phill W.
Sep 15 '06 #4
i'm sorry, i suppose i should have been more exact; my actual interface
method is defined as

Interface IBaseContractControl
Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
End Interface

(i used an empty paramter list to simplify the example, my apologies)

this (in theory) allows me to use a single function located in
MyUserControl which can satisfy the interface and be directly wired up
to the event. i'm just not sure how to wire it up at run time from
MyParentControl. (instead of, for example, calling "AddHandler
MyButton.Click, AddressOf ButtonClick" in MyUserControl's constructor)

-keith

Phill W. wrote:
ke*************@gmail.com wrote:
interface IMyInterface
sub buttonClick()
end interface

class MyParentControl
protected MyButton as Button
end class

class MyUserControl
inherits MyParentControl
implements IMyInterface

sub buttonClick() implements IMyInterface.buttonClick
end sub
end class

-------------------------------------------------

here's the catch. i want MyUserControl.buttonClick to also handle
MyButton.Click(), and i want the code that wires up that event to be in
MyParentControl, not MyUserControl.

is it possible?

The biggest problem here is that event handlets have a distinctive
signature - "name( Object, EventArgs )" - that your Interface method
doesn't have. You're going to have to have a /separate/ routine that
does the actual work and call it from (a) the button's "click" handler
and (b) the method that implements the Interface.

Class MyParentControl

Protected WithEvents MyButton as Button

Sub New()
MyButton = New Button
With MyButton
...
End With
End Sub

Protected Sub buttonClick()
' code for dealing with "Click" goes here
End Sub

Private Sub MyButton_Click(sender As Object, e As EventArgs) _
Handles MyButton.Click

Me.buttonClick()

End Sub

End Class
Class MyUserControl
Inherits MyParentControl
Implements IMyInterface

Sub New()
MyBase.New()

End Sub

Private Sub Something() _
Implements IMyInterface.buttonClick

MyBase.buttonClick()

End Sub

End Class

HTH,
Phill W.
Sep 15 '06 #5
ke*************@gmail.com wrote:
i'm sorry, i suppose i should have been more exact; my actual interface
method is defined as

Interface IBaseContractControl
Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
End Interface

(i used an empty paramter list to simplify the example, my apologies)

this (in theory) allows me to use a single function located in
MyUserControl which can satisfy the interface and be directly wired up
to the event. i'm just not sure how to wire it up at run time from
MyParentControl. (instead of, for example, calling "AddHandler
MyButton.Click, AddressOf ButtonClick" in MyUserControl's constructor)
You will need to add an event to your user control that is exposed to
your controls parent. Then inside your button click, you need to raise
that event.

Sep 15 '06 #6
thank you everyone for the help.

here is how i solved it:

this method was added to MyParentControl

Private Function AddInterfaceHandler(ByVal EventName As String, ByVal
InterfaceMethod As String) As Boolean

Dim objType As Type
Dim deleg As System.Delegate
Dim evInfo As System.Reflection.EventInfo

Try
objType = GetType(BaseContractControl(Of Thelper, Tsps))
evInfo = objType.GetEvent(EventName)
deleg =
System.Delegate.CreateDelegate(evInfo.EventHandler Type, Me,
InterfaceMethod)
evInfo.AddEventHandler(Me, deleg)

Return True
Catch ex As Exception
' derived must have not implemented the interface

Return False
End Try

End Function

this function could easily be made to wire directly to the objects
firing the events (i.e. the button) if you add a "Target as Object"
parameter and change the final "AddEventHandler" call to pass that
object rather than Me.

but since i've adjusted MyParentControl to handle the button click
event directly and then fire off its own event, i could hardcode that
call.

-keith

Chris Dunaway wrote:
ke*************@gmail.com wrote:
i'm sorry, i suppose i should have been more exact; my actual interface
method is defined as

Interface IBaseContractControl
Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
End Interface

(i used an empty paramter list to simplify the example, my apologies)

this (in theory) allows me to use a single function located in
MyUserControl which can satisfy the interface and be directly wired up
to the event. i'm just not sure how to wire it up at run time from
MyParentControl. (instead of, for example, calling "AddHandler
MyButton.Click, AddressOf ButtonClick" in MyUserControl's constructor)

You will need to add an event to your user control that is exposed to
your controls parent. Then inside your button click, you need to raise
that event.
Sep 15 '06 #7

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

Similar topics

31
2885
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
2
9150
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
3
2700
by: Brett Hall | last post by:
I have a VB.NET interface that my managed C++ code is to implement. I seem to be stuck implementing an event defined in that interface. Does anyone have a simple code snippet that will show me...
4
2836
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
13
3023
by: CJ Taylor | last post by:
Alright on a DataRow you have the BeginEdit method Where can I catch an event that tells me that method is being fired or does one exist? Thanks, -CJ
6
1783
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged...
0
1050
by: ZA_AJR | last post by:
Hi All I have a class (aClass, derived from aBaseClass) with implements an interface IMyInterface. aClass also has a private member of type aNotherClass which raises various events. I would like...
7
1964
by: Sin Jeong-hun | last post by:
I've using thread a lot in C#, but all of them were just single method with no return value or paramenters. It may sound bizarre but, can't it be a class? I'm writing an Windows application that...
7
1930
by: Jeff | last post by:
I need a way to do the following and cannot seem to find a solution via google. 1. Have a method from the main app to get all open forms 2. Check each open form for a public method 3. If this...
0
7287
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
7348
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...
1
7006
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
7467
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
5592
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,...
1
5021
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...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
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.