473,569 Members | 2,782 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.bu ttonClick
end sub
end class

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

here's the catch. i want MyUserControl.b uttonClick 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 2437
>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.bu ttonClick
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.bu ttonClick
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.bu ttonClick
end sub
end class

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

here's the catch. i want MyUserControl.b uttonClick 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.bu ttonClick

MyBase.buttonCl ick()

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 IBaseContractCo ntrol
Sub ButtonClick(ByV al sender As System.Object, ByVal e As
System.EventArg s)
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.bu ttonClick
end sub
end class

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

here's the catch. i want MyUserControl.b uttonClick 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.bu ttonClick

MyBase.buttonCl ick()

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 IBaseContractCo ntrol
Sub ButtonClick(ByV al sender As System.Object, ByVal e As
System.EventArg s)
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 AddInterfaceHan dler(ByVal EventName As String, ByVal
InterfaceMethod As String) As Boolean

Dim objType As Type
Dim deleg As System.Delegate
Dim evInfo As System.Reflecti on.EventInfo

Try
objType = GetType(BaseCon tractControl(Of Thelper, Tsps))
evInfo = objType.GetEven t(EventName)
deleg =
System.Delegate .CreateDelegate (evInfo.EventHa ndlerType, Me,
InterfaceMethod )
evInfo.AddEvent Handler(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 "AddEventHandle r" 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 IBaseContractCo ntrol
Sub ButtonClick(ByV al sender As System.Object, ByVal e As
System.EventArg s)
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
2898
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 using myclass.len() instead of myclass.__len__() really cause Python considerable harm? As much as I adore Python, I have to admit, I find this to...
2
9163
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 for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not...
3
2711
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 the basics of what I need to implement? I've seen all the MSDN articles on implementing events in managed C++ and I've gotten events to work without...
4
2839
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. '**************************************************************************** ' Issues '****************************************************************************
13
3030
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
1792
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 resource? I understand the basic definition for managed resources are resources that the CLR manage and unmanged resources are resources that the CLR...
0
1057
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 a specific method within aClass to handle the event raised by the object of type aNotherClass AS WELL as implementing the method of the interface....
7
1972
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 processes inputs from a serial port. The serial port data is processed when DataReceived event occurs from the SerialPort instance. If the...
7
1937
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 public method exists execute it which will result in the displayed form being updated. There will be no data loss as the forms will only contain...
0
7697
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...
0
7924
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
8120
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
7672
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
6283
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...
1
5512
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...
1
2113
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 we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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.