Solved

the event of the class module

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now