?
Solved

Handles Clause requires a WithEvents variable

Posted on 2008-10-23
6
Medium Priority
?
1,750 Views
Last Modified: 2012-06-27
Hi

I have a app that is using a third party library to connect a device to a serial port. All is working well if I stay with one form, but I need to maintain the open serial connection across forms.

I have a variable defined in a module

Public WithEvents ph as ProtocolHandler

But when I try to add a sub on another form that Handles ph.ReaderDateTimeChanged I get a compile error

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

If I try to define ph locally as Private WithEvents ph as ProtocolHandler, the error goes away, but the protocols setup in the module are lost.

Thanks
0
Comment
Question by:nsfranklin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Omego2K
ID: 22786515
Have you tried declaring it private, but making a property to return the value?
0
 

Author Comment

by:nsfranklin
ID: 22786610
sorry could you provide an example
0
 
LVL 14

Expert Comment

by:Omego2K
ID: 22786780
Something like this?
Private WithEvents ph as ProtocolHandler
 
Public ReadOnly Property SomeName As ProtocolHandler
   Get
       return ph
   End Get
End Property

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 22788545
You can assign your Form variable to be the same one as the Module in the Load() event like this:

Module Module1

    Public WithEvents ph As ProtocolHandler

End Module

Public Class Form1

    Private WithEvents ph As ProtocolHandler

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ph = Module1.ph
    End Sub

End Class
0
 

Author Comment

by:nsfranklin
ID: 22795736
Thanks Idle Mind, your solution worked great, sorry Omego2K
0
 

Author Closing Comment

by:nsfranklin
ID: 31509157
Thanks for your prompt reply and solution.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question