Solved

the event of the class module

Posted on 1998-08-01
3
242 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
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…

763 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

11 Experts available now in Live!

Get 1:1 Help Now