Solved

the event of the class module

Posted on 1998-08-01
3
257 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating VB6 Function 19 65
MS Access - Capture pressed key onclick 4 41
Adding to a VBA? 6 77
transition to visual .net from vb6 5 37
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

840 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