Solved

the event of the class module

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

730 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