Solved

Handles Clause requires a WithEvents variable

Posted on 2008-10-23
6
1,712 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
  • 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now