zimmerma379
New member
- Joined
- Mar 8, 2010
- Messages
- 2
- Programming Experience
- 3-5
I am having trouble implementing an Event from an Interface in a ClassLibrary.
Here's the scenario:
o I have 2 projects -- a ClassLibrary "CL1" and a Windows Application.
The ClassLibrary:
o Contains a Friend Interface "I1" and a Public Class "C1".
o The Interface defines one Event "E1" and one Property "P1".
o The Class "C1" implements the Interface "I1".
The Windows Application:
o Instantiates the Class "C1" from the ClassLibrary "CL1" and attempts to handle its event "E1".
Problem:
Everything compiles fine. The “C1.P1” property functions just fine in the Windows Application. But the attempt to handle the “C1.E1” event in the Windows Application throws the following exception:
An unhandled exception of type 'System.MethodAccessException' occurred in WindowsApplication.exe
Additional information: E1EventHandler..ctor(System.Object, IntPtr)
If I change the Interface “I1” in the ClassLibrary to be PUBLIC the exception is NOT thrown. But I don’t want to expose the Interface “I1” outside of the ClassLibrary as it is only used within the ClassLibrary project.
My hunch is that the auto-generated “E1EventHandler” method is marked as Friend from the “I1” Interface and therefore it does not have visibility outside the ClassLibrary? I tried messing around with a Delegate in the ClassLibrary but could not get things to work. I’m not well versed in vb.NET Events & Delegates.
Is there anyway to properly use the Interface “I1” to enforce implementation of the “E1” Event by the Class “C1” within the ClassLibrary but without exposing this Interface outside of the ClassLibrary?
Here’s the sample code:
Project #1 (Class Library) "CL1.vbproj":
File I1.vb:
File C1.vb:
Project #2 (Windows Application):
Make reference to Project #1 "CL1" above.
File Form1.vb:
Here's the scenario:
o I have 2 projects -- a ClassLibrary "CL1" and a Windows Application.
The ClassLibrary:
o Contains a Friend Interface "I1" and a Public Class "C1".
o The Interface defines one Event "E1" and one Property "P1".
o The Class "C1" implements the Interface "I1".
The Windows Application:
o Instantiates the Class "C1" from the ClassLibrary "CL1" and attempts to handle its event "E1".
Problem:
Everything compiles fine. The “C1.P1” property functions just fine in the Windows Application. But the attempt to handle the “C1.E1” event in the Windows Application throws the following exception:
An unhandled exception of type 'System.MethodAccessException' occurred in WindowsApplication.exe
Additional information: E1EventHandler..ctor(System.Object, IntPtr)
If I change the Interface “I1” in the ClassLibrary to be PUBLIC the exception is NOT thrown. But I don’t want to expose the Interface “I1” outside of the ClassLibrary as it is only used within the ClassLibrary project.
My hunch is that the auto-generated “E1EventHandler” method is marked as Friend from the “I1” Interface and therefore it does not have visibility outside the ClassLibrary? I tried messing around with a Delegate in the ClassLibrary but could not get things to work. I’m not well versed in vb.NET Events & Delegates.
Is there anyway to properly use the Interface “I1” to enforce implementation of the “E1” Event by the Class “C1” within the ClassLibrary but without exposing this Interface outside of the ClassLibrary?
Here’s the sample code:
Project #1 (Class Library) "CL1.vbproj":
File I1.vb:
VB.NET:
Friend Interface I1
Event E1()
Property P1() As String
End Interface
File C1.vb:
VB.NET:
Public Class C1
Implements I1
Public Event E1() Implements I1.E1
Public Property P1() As String Implements I1.P1
Get
End Get
Set(ByVal Value As String)
End Set
End Property
End Class
Project #2 (Windows Application):
Make reference to Project #1 "CL1" above.
File Form1.vb:
VB.NET:
Public Class Form1
Inherits System.Windows.Forms.Form
'Windows Form Designer generated code (not shown)
Dim WithEvents o As New CL1.C1
Private Sub o_E1() Handles o.E1
End Sub
End Class
Last edited: