Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with Events 1

Status
Not open for further replies.

williamu

Programmer
Apr 8, 2002
494
GB
Hi all,

I've created a class the defines an Event. This event is raised when a configuration file cannot be found.

I've declared in the main sdi form:

Dim WithEvents m_Config as Config

In the Class's Initialize event I check to see if this file exists. If it doesn't I raise the event.

Private Sub Class_Initialize()
Set fsO = CreateObject("Scripting.FileSystemObject")
If (Not fsO.FileExists("test.cfg")) Then
RaiseEvent CfgIOError("Config file is Missing!")
Else
' Todo read configuration
End If
End Sub

Back in the Main SDI form I've added:

Private Sub m_Config_CfgIOError(m_Error As String)
m_Status.Panels(1).Text = m_Error
End Sub

If I use a breakpoint I can see that this event is being fired but it is never intercepted in the main form! The class in question is being created in the form's Load event:

Set m_Config = New Config

Can anyone tell me where I'm going or what I'm doing wrong, please. This is driving me crazy! I guess it may well be something trivial but I can't see it. According to the MSDN topic "Adding Events to a Class", I'm on the right track.

--
Thanks,

William.
 
How is your event declared in the class?

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I had a something similar recently....

are you sure that you don't have like this
Code:
'form level variable...

private withevents objMyThing as mything

public sub SomeSub
dim objMyThing as new mything

....
end sub

The second declaration doesn't give an error, but does mean that events aren't raised by the object (when in sub Somesub)



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Matt I have the following declaration in the Class File:

Public Event CfgIOError(m_Error As String)
The General Declarations contains:

Private WithEvents m_Config As Config

and the Form_Load has:

Set m_Config = New Config

--
William
 
hmm

Looks OK

Have you tried starting a new project and adding this class to that. you then have a simple form and button to test it... You may find it easier to see what you get then

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Nope, but I'll give it a go.

--

William
Software Engineer
ICQ No. 56047340
 
Ok, I've created the simplest of apps to test the Events but still no joy! I'm well confused.

If anyone is willing to review this code to see what I've done (wrong) then I'll be willing to send it. I can be reached via email at wgu_@_forensic-labs.co.uk (minus the two _ of course).

I'll email it by return. In the meantime I'm off to scratch my head in confusion.

--



William
Software Engineer
ICQ No. 56047340
 
Having looked at this....

It seems, empirically atleast, that events raised in the class_initialize event don't get raised properly.

You can get round this by moving the Fileexists test to another public sub and calling so the code looks like

Code:
Private Sub Class_Initialize()
    Set fsO = CreateObject("Scripting.FileSystemObject")
end sub

Public Sub ConfigExists()
    If (Not fsO.FileExists("test.cfg")) Then
        RaiseEvent CfgIOError("Config file is Missing!")
    Else
        ' Todo read configuration
    End If
End Sub

Unfortunately, I can't find any documentation on why events can't be raised for the class_initialize event, although I suspect it is because the interfaces aren't full created by that time.




Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top