I have recently found out about VBIDE library, but I don't quite grap all of it's capabilities yet. I want to make a method that will instantiate correct Class based on classes found in my Project. Classes that fit as candidates to be instantiated Implement
InterfaceA
, and the exact class that I want to instantiate has property MType
and it returns Enumerated Value
Correct
.
我最近发现了VBIDE库,但我还没有完全掌握它的所有功能。我想创建一个方法,根据我的Project中找到的类来实例化正确的Class。适合作为实例化候选者的类实现InterfaceA,我想要实例化的确切类具有属性MType,并返回Enumerated Value Correct。
So to sumarize how do I iterate over classes defined in my project in order to find Class that returns Correct
for Property MType
, and Instantiate that Class.
因此,为了找到我的项目中定义的类,以便找到返回Correct for Property MType的Class,并实例化该Class,以便进行sumarize。
Thus far I know that I can Iterate over my modules with following code:
到目前为止,我知道我可以使用以下代码迭代我的模块:
Dim Part As VBComponent
For Each Part In Application.VBE.ActiveVBProject.VBComponents
Debug.Print Part.Name
Next Part
What I am missing now how do I iterate over methods/properties of each class to find out what these mehods return?
我现在缺少的是如何迭代每个类的方法/属性以找出这些方法返回的内容?
Here is method I need to find, it varies from class to class by value it returns:
这是我需要找到的方法,它按类返回的类不同:
Public Property Get InterfaceA_MType() As Model_Types
IModel_MType = Severity
End Property
So as you can see this Property is pretty simple I am to assume that it will return same value all the time.
因此,您可以看到此属性非常简单,我假设它将一直返回相同的值。
UPDATE: Per Dough Gancy's observation part of the answer is located in here I can use ProcBodyLine(InterfaceA_MType)
and ProcCountLines(InterfaceA_MType)
to iterate over the procedure lines, match those lines witch one that has IModel_MType = Correct
.
更新:Per Dough Gancy的观察部分答案位于此处我可以使用ProcBodyLine(InterfaceA_MType)和ProcCountLines(InterfaceA_MType)迭代过程行,匹配那些具有IModel_MType = Correct的行。
This leaves out only instantiating the Class based on the Code Module. how do I do that?
这样就不会仅基于代码模块实例化Class。我怎么做?
1 个解决方案
#1
2
First off, this doesn't iterate over your classes, it iterates over all modules in your file.
首先,这不会迭代您的类,它会迭代您文件中的所有模块。
Dim Part As VBComponent For Each Part In Application.VBE.ActiveVBProject.VBComponents Debug.Print Part.Name Next Part
If you want to iterate over just the class modules, you'll need to check the component type.
如果要仅迭代类模块,则需要检查组件类型。
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 particular method in a code module, you'll need to use the Find method of the CodeModule object.
现在,要在代码模块中查找任何特定方法,您需要使用CodeModule对象的Find方法。
Of special note, is the fact that startline
, endline
, startcol
, and endcol
are all passed by reference and are effectively "out" parameters.
特别值得注意的是,startline,endline,startcol和endcol都是通过引用传递的,并且是有效的“out”参数。
So, that code would 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, in order to create an instance of a class, it's going to necessarily be a global instance... which kind of smells, but whatever. You've not really given us your end goal. I have a feeling that your problem is better solved via some good OOP programming, but that's not the question you asked.
最后,为了创建一个类的实例,它必然是一个全局实例......哪种气味,但无论如何。你没有真正给我们你的最终目标。我觉得通过一些好的OOP编程可以更好地解决你的问题,但这不是你问的问题。
To get an instance of the class to work with, you'll need to dynamically create a module and procedure that instantiates an instance of that class. Perhaps even re-writes the calling code on the fly.
要获取要使用的类的实例,您需要动态创建实例化该类实例的模块和过程。甚至可能即时重写调用代码。
So, in this dynamically generated module, you'll need to write something like this.
因此,在这个动态生成的模块中,您需要编写类似这样的内容。
Public dynamic As ClassType
Public Sub InitalizeDynamic()
Set dynamic = new ClassType
End Sub
Then, somewhere else, you would call the plain jane module sub with Application.Run
.
然后,在其他地方,您将使用Application.Run调用plain jane模块子。
Public Sub Test1()
Application.Run "VBAProject.Module1.IntializeDynamic"
End Sub
So, that finally you could use the instance of dynamic.
所以,最后你可以使用动态的实例。
foo = dynamic.InterfaceA_MType()
If you carefully read the questions I linked to with other answers of mine, you should be able to work out a solution. Everything you need to know is in the combination of those two answers.
如果你仔细阅读我链接的问题和我的其他答案,你应该能够找到解决方案。您需要知道的一切都是这两个答案的组合。
#1
2
First off, this doesn't iterate over your classes, it iterates over all modules in your file.
首先,这不会迭代您的类,它会迭代您文件中的所有模块。
Dim Part As VBComponent For Each Part In Application.VBE.ActiveVBProject.VBComponents Debug.Print Part.Name Next Part
If you want to iterate over just the class modules, you'll need to check the component type.
如果要仅迭代类模块,则需要检查组件类型。
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 particular method in a code module, you'll need to use the Find method of the CodeModule object.
现在,要在代码模块中查找任何特定方法,您需要使用CodeModule对象的Find方法。
Of special note, is the fact that startline
, endline
, startcol
, and endcol
are all passed by reference and are effectively "out" parameters.
特别值得注意的是,startline,endline,startcol和endcol都是通过引用传递的,并且是有效的“out”参数。
So, that code would 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, in order to create an instance of a class, it's going to necessarily be a global instance... which kind of smells, but whatever. You've not really given us your end goal. I have a feeling that your problem is better solved via some good OOP programming, but that's not the question you asked.
最后,为了创建一个类的实例,它必然是一个全局实例......哪种气味,但无论如何。你没有真正给我们你的最终目标。我觉得通过一些好的OOP编程可以更好地解决你的问题,但这不是你问的问题。
To get an instance of the class to work with, you'll need to dynamically create a module and procedure that instantiates an instance of that class. Perhaps even re-writes the calling code on the fly.
要获取要使用的类的实例,您需要动态创建实例化该类实例的模块和过程。甚至可能即时重写调用代码。
So, in this dynamically generated module, you'll need to write something like this.
因此,在这个动态生成的模块中,您需要编写类似这样的内容。
Public dynamic As ClassType
Public Sub InitalizeDynamic()
Set dynamic = new ClassType
End Sub
Then, somewhere else, you would call the plain jane module sub with Application.Run
.
然后,在其他地方,您将使用Application.Run调用plain jane模块子。
Public Sub Test1()
Application.Run "VBAProject.Module1.IntializeDynamic"
End Sub
So, that finally you could use the instance of dynamic.
所以,最后你可以使用动态的实例。
foo = dynamic.InterfaceA_MType()
If you carefully read the questions I linked to with other answers of mine, you should be able to work out a solution. Everything you need to know is in the combination of those two answers.
如果你仔细阅读我链接的问题和我的其他答案,你应该能够找到解决方案。您需要知道的一切都是这两个答案的组合。