Solved

Handles Clause requires a WithEvents variable

Posted on 2008-10-23
6
1,735 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 86

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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