Hi
I'm new to VBA. I'm having a very weird problem which I think is down to how the Visual Basic editor works but don't know enough to track further...has anyone seen something similar before?
My project has an interface class iFoo. This is implemented by several classes cBarA, cBarB and cBarC.
At runtime a factory function creates the appropriate class according to an input parameter. Ie:
------------
iFoo
------------
Option Explicit
Public Sub Test()
End Sub
------------
--------------
cBarA
--------------
Option Explicit
Implements iFoo
Public Sub iFoo_Test()
Debug.Print "I am BarA"
End Sub
------------
--------------
cBarB
--------------
Option Explicit
Implements iFoo
Public Sub iFoo_Test()
Debug.Print "I am BarB"
End Sub
------------
--------------
cBarC
--------------
Option Explicit
Implements iFoo
Public Sub iFoo_Test()
Debug.Print "I am BarC"
End Sub
------------
------------
Option Explicit
Public Function fooFactory (strType as String) As iFoo
Dim oOutput as iFoo
If strType = "A" then
set oOutput = new cBarA
Else if strType = "B" then
set oOutput = new cBarB
Else if strType = "C" then
set oOutput = new cBarC
End If
set fooFactory = oOutput **** this line fails in certain states, see below
End Function
Public Sub TestFoo()
Dim v as iFoo
set v = fooFactory("A")
v.Test
set v = fooFactory("B")
v.Test
set v = fooFactory("C")
v.Test
End Sub
(Real World Details: The real app reads a log file and the classes represent different types of events in the logs. Each line in the log is passed to the factory which returns the appropriate event)
The worksheet contains several sheets with test input. If I open the worksheet and try to run the test I get a type mismatch error when the created object is assigned to the interface type variable. However if I go to any of the class modules that implement the interface, remove the "implements iFoo" line and retype it then run the test on exactly the same data it works!
There are several inputs sheets and I get the same results with all of them.
It doesn't matter if I run the test once, twice or n times after opening, it won't work until I have removed and replaced the implements line in any one of the class modules. Compiling doesn't seem to make any difference: if I compile after opening without making the change it doesn't fix it, but if I compile after making the change it doesn't break it either. If I save the project and worksheet in the working state (ie after replacing the implements line) it will be broken again when I reopen it.
It looks like when the file is first opened for some reason VBA doesn't pick up the Implements statement and throws a type mismatch, then reentering the statement causes the interpreter to update it and the statement is picked up. I assume the code is compiled/interpreted differently after a change in the editor than when the file is saved, but I don't know enough about the difference to help me solve me problem.
Can anyone give me any pointers?
Many thanks (and if you've read this far well done!)
Liz
Bookmarks