Iterating over methods defined in a module
I recently learned about the VBIDE library, but can't fully appreciate all the possibilities yet. I want to create a method that will create the correct class based on the classes found in my project. Classes that are good candidates for instantiation Implement
InterfaceA
, and the exact class I want to instantiate has a property MType
and returns Enumerated Value
Correct
.
So to summarize how I iterate through the classes defined in my project to find the class that returns Correct
for the property MType
and create that class.
So far, I know I can iterate over my modules with the following code:
Dim Part As VBComponent
For Each Part In Application.VBE.ActiveVBProject.VBComponents
Debug.Print Part.Name
Next Part
What am I missing right now, how do I iterate over the methods / properties of each class to see what these mehods are returning?
Here is the method I need to find, it changes from class to class according to its values:
Public Property Get InterfaceA_MType() As Model_Types
IModel_MType = Severity
End Property
So as you can see this property is quite simple, I have to assume that it will return the same value all the time.
UPDATE: Some of the observation I got from Dough Gancy is in here
I can use ProcBodyLine(InterfaceA_MType)
it ProcCountLines(InterfaceA_MType)
to iterate over the lines of procedures that match those lines that have IModel_MType = Correct
.
This only excludes creating an instance of a class based on a code module. how to do it?
source to share
First, it doesn't iterate over your classes, iterate over all the modules in your file.
Dim Part As VBComponent
For Each Part In Application.VBE.ActiveVBProject.VBComponents
Debug.Print Part.Name
Next Part
If you only want to iterate over the modules of a class, you will need to check the type of the component.
Dim Part As VBComponent
For Each Part In Application.VBE.ActiveVBProject.VBComponents
If Part.Type = vbext_ct_ClassModule Then
Debug.Print Part.Name
End If
End If
Now, to find any specific method in a code module, you need to use the Find Method of the CodeModule object .
Of particular note is thatstartline
,
endline
,
startcol
and
endcol
all passed by reference and are effectively "off" .
So, this code will look something like this.
Dim startLine As Long
Dim endLine As Long
Dim startCol As Long
Dim endCol As Long
startLine = 1
startCol = 1
endLine = -1
endCol = -1
If Part.Find("Public Property Get InterfaceA_MType()", startLine, startCol, endLine, endCol) Then
' do something
End If
Finally, to instantiate a class, it will definitely become a global instance ... whatever smells, but whatever. You didn't really give us your ultimate goal. I have a feeling that your problem is better solved with some good OOP programs, but that is not the question you asked.
To get an instance of a class to work, you need to dynamically create a module and a procedure that instantiates that class. Maybe even rewrites the calling code on the fly.
So, in this dynamically generated module, you need to write something like this.
Public dynamic As ClassType
Public Sub InitalizeDynamic()
Set dynamic = new ClassType
End Sub
Then, somewhere else, you call the simple jane module withApplication.Run
.
Public Sub Test1()
Application.Run "VBAProject.Module1.IntializeDynamic"
End Sub
So finally you can use a dynamic instance.
foo = dynamic.InterfaceA_MType()
If you read carefully the questions I've linked to my other answers, you should be able to work out a solution. All you need to know is the combination of these two answers.
source to share