Solved

the event of the class module

Posted on 1998-08-01
3
253 Views
Last Modified: 2010-05-03
Hi

Can you give me a very simple example to show me how to write an event in a class module?
I know a little bit about that: declare the event in the generea section of the class, raise the event in somewere in some procedures of the class. However where and how to write the code for the event procedure itself?
I also confused what the difference between the event and the function in a class module :-(
0
Comment
Question by:username1
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Llandr earned 50 total points
ID: 1467430
Ok. I can see your confusion. The difference between an event and a function is that the function in the class module allows the class itself to perform an action, the event is a call-out, it allows another entity to respond to the function.
Consider this example:

<------ Code start--------->

<Make a class named CTextReader>
Option Explicit

'This class reads text from a file line by line

Public Event LineRead(aLine As String)

Public Sub ProcessFile()
  Dim buffer As String

  Open "c:\Test.txt" For Input As 1
  While Not EOF(1)
    Line Input #1, buffer
    RaiseEvent LineRead(buffer)
  Wend
  Close 1
End Sub
<End of CTextReader>

<Make a Form with one button>

<Form start>
Option Explicit

Public WithEvents aTextReader As CTextReader


Private Sub aTextReader_LineRead(aLine As String)
  MsgBox "Line:" & aLine & " read"
End Sub

Private Sub Command1_Click()
  aTextReader.ProcessFile
End Sub

Private Sub Form_Load()
  Set aTextReader = New CTextReader
End Sub
<Form end>
<--------- Code ends--------->

You see, you say that I will trigger this event and the client chooses how to respond to it (or if to respond at all).
Hope this clarifies it a bit.




0
 

Author Comment

by:username1
ID: 1467431
Hi Llandr

That is a great answer to me, thank you very much.
My understanding is:
1. in a class, declare the event name and put raiseevent in a method.
2. now use the class (say in a form), write the event code and then use the method above to raise it :-)
However, why you use Public instead of DIM or Private in the following line:
Public WithEvents aTextReader As CTextReader

Cheers.

Username1
0
 
LVL 2

Expert Comment

by:Llandr
ID: 1467432
You are correct, I naturally should have written Private (dim works but I don't find it "kosher", better to use private).
Public seems to work also and could be used from the outside.

You have understood points 1 and 2 correctly. :-)
The example is a bit oversimplified but the idea is the same.
Usually you use the raiseevent when you write controls or servers that someone else uses.

Regards:
Llandr
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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