Solved

Handles Clause requires a WithEvents variable

Posted on 2008-10-23
6
1,718 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

786 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