Hi Thanks,
Is there an equivalent to EventArgs.Empty for CancelEventArgs? What actually are we passing as EventArgs.Empty...?
Main Topics
Browse All TopicsHi,
I had some code which was working fine, but now I have turned Option Strict On, I am getting a build error.
Option Strict On disallows implicit conversion from System.EventArgs to System.ComponentModel.Canc
VB suggests I chane the line:
RaiseEvent AfterPasswordChanged(Me, EventArgs.Empty)
to
RaiseEvent AfterPasswordChanged(Me, CType(EventArgs.Empty, CancelEventArgs))
But I dont understand why this is. CancelEventArgs inherit from EventArgs, so isnt this a narrowing conversion because there may be properties/methods in the CancelEventArgs which are not in the base class?
If anyone can explain I would appreciate it.
Regards
P
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>>
What actually are we passing as EventArgs.Empty...?
<<
Nothing, nil, zilch. It "represents an event with no event data .... The value of Empty is a read-only instance of EventArgs equivalent to the result of calling the EventArgs constructor".
CancelEventArgs, on the other hand, "provides data for a cancelable event": that's in its .Cancel property.
I suppose the real point here is not the one specifically highlighted by changing Option Strict from Off to On: of whether on line 23 you should be passing EventArgs.Empty or New CancelEventArgs. It is whether, on line 6, you should be declaring AfterPasswordChanged As CancelEventHandler or As EventHandler. Given that - as you were originally passing EventArgs.Empty and it worked - it seems you don't need AfterPasswordChanged to be "a cancelable event", perhaps I should have said that the proper method to correct this is not to do a conversion, but to change the code [line 6] to
Public Event AfterPasswordChanged As EventHandler
But it all depends what you're after. Code that is technically "perfect" or just code that works ;-)
Roger
No, on reflection, I don't think so. Because, as you say, it's a narrowing conversion, although Option Strict Off may have allowed that bit of code to pass when it was being compiled, I think an error would have been thrown when the code was actually hit. A test seems to confirm that. Neither the explicit CType conversion suggested by VB, nor (with Option Strict Off) an implicit conversion gets past it.
If, indeed, it WAS working before Option Strict was On, I don't know how. Was this the only change highlighted when you turned Option Strict On? Or, more pointedly, have you made any other changes to the code since it was working with Option Strict Off? If not, I would suggest turning Option Strict Off again, and debugging with values which would cause that line to be hit and see what happens.
Roger
Business Accounts
Answer for Membership
by: SanclerPosted on 2008-03-12 at 17:18:00ID: 21112148
The reason you get the error is because [line 6] you declare AfterPasswordChanged As CancelEventHandler and CancelEventHandler takes as its second argument e As CancelEventArgs not e As EventArgs. So, without Implicit conversion (which Option Strict On disallows) the signatures don't match.
But I agree that the suggestion for how to deal with it is not a particularly good one. I imagine, however, that it's just the result of some automatic process which is based on "this is wrong, what does it need changing to, what method will make the change" rather than the sort of more principled consideration that your post raises. The proper method to correct this is not to do a conversion, but to change the code [line 23] to
RaiseEvent AfterPasswordChanged(Me, New CancelEventArgs)
Roger