-
Possible bug?
Hi there,
I think I might have discovered a bug in .NET. I am having a problem implementing
an event defined in a separate interface. I created 2 projects. The first
is a class library. The second is a windows application. The class library
contains my business interface class and a class that implements the interface.
The windows application contains my UI. The form in my UI cannot capture
the event generated from the business object. When I attempt to run the
application, I receive the following error in the form load event:
An unhandled exception of type ’System.MethodAccessException’ occurred in
WindowsApplication 4.exe.
If the form is included in the same project as the interface and business
class, everything runs successfully. However if the interface and business
object are in another project it no longer works. I don’t want to post my
whole program, so here is a small sample application that implements the
problem. Am I doing something wrong or is this a bug in .NET? I am using
RC1.
Project 1 contains…
Friend Interface IBusObj
Event Walk(ByVal Distance As Integer)
ReadOnly Property DistanceWalked() As Integer
End Interface
Public Class Person
Implements IBusObj
Private mintDistanceWalked As Integer
Public Event Walked(ByVal intDistance As Integer) Implements IBusObj.Walk
Private ReadOnly Property DistanceWalked() As Integer Implements IBusObj.DistanceWalked
Get
DistanceWalked = mintDistanceWalked
End Get
End Property
Public Sub Walk(ByVal Distance As Integer)
mintDistanceWalked += Distance
RaiseEvent Walked(Distance)
End Sub
End Class
Project 2 contains…
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents mobjPerson As ClassLibrary1.Person
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
mobjPerson = New ClassLibrary1.Person()
End Sub
Private Sub btnWalk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnWalk.Click
mobjPerson.Walk(CInt(txtDistance.Text))
End Sub
Private Sub onWalk(ByVal Distance As Integer) Handles mobjPerson.Walked
MsgBox("Person walked " & Distance)
End Sub
End Class
-
Re: Possible bug?
Angela,
>Am I doing something wrong or is this a bug in .NET?
The problem is in your code, but I guess you could call it a bug that
the VB compiler doesn't catch this at compile time.
>Friend Interface IBusObj
Since the interface is declared as Friend, it's not accessible from
Project 2. You have to make the interface, or at least the event
delegate, public. So either change it to
Public Interface IBusObj
....
or to
Public Delegate Sub WalkEventHandler(ByVal Distance As Integer)
Friend Interface IBusObj
Event Walk As WalkEventHandler
....
Mattias
===
Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.net/dotnet/
-
Re: Possible bug?
That sort of makes sense but in my application, the interface also contains
many properties and methods that work:
'code from my real application
Friend Interface IBusinessObject
Event Valid(ByVal IsValid As Boolean)
ReadOnly Property IsValid() As Boolean
ReadOnly Property IsNew() As Boolean
ReadOnly Property IsDirty() As Boolean
Sub Load()
Sub Save()
Sub Delete()
Sub Clear()
End Interface
All the methods and properties work fine across projects. Why is it that
only the event did not work?
Angela
Mattias Sjögren <mattias.dont.want.spam@mvps.org> wrote:
>Angela,
>
>>Am I doing something wrong or is this a bug in .NET?
>
>The problem is in your code, but I guess you could call it a bug that
>the VB compiler doesn't catch this at compile time.
>
>
>>Friend Interface IBusObj
>
>Since the interface is declared as Friend, it's not accessible from
>Project 2. You have to make the interface, or at least the event
>delegate, public. So either change it to
>
>Public Interface IBusObj
>....
>
>or to
>
>Public Delegate Sub WalkEventHandler(ByVal Distance As Integer)
>
>Friend Interface IBusObj
> Event Walk As WalkEventHandler
>....
>
>
>
>Mattias
>
>===
>Mattias Sjögren (VB MVP)
>mattias @ mvps.org
>http://www.msjogren.net/dotnet/
-
Re: Possible bug?
Angela,
>All the methods and properties work fine across projects. Why is it that
>only the event did not work?
As long as the methods and properties are public members of the
implementing class, the interface is never involved in the calls,
that's why you don't get a problem with them.
If, on the other hand, they were private, you wouldn't be able to
access them without casting to the interface type. And since the
interface is private, you can't access them at all. That's the case
with the DistanceWalked property in the code you originally posted.
The problem with the event is that VB generates a delegate class for
the event for you, and nests it within the interface. So if the
interface is made internal, so is effectively the delegate class.
That's why you get the method access error when trying to call the
constructor of the delegate for handling the event.
Mattias
===
Mattias Sjögren (VB MVP)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks