Create a new object using the text name of the class

Is there a way to set the object to a new instance of the class using the text name of the class?

I will have a class library, and depending on some other variable, I want to get one of these classes at runtime.

For example, I have "CTest1", "CTest2", "CTest3"

I would have a function similar to the one below

Function GetTestClass(lngClassNo as long) as Object Dim strClassName as String strClassName = "CTest" & CStr(lngClassNo) Set GetTestClass = New instance of class(strClassName) End Function 
+7
vba excel-vba excel
source share
6 answers

There is no reflection in VBA, so I don't think it is possible. You should do something like the following, I'm afraid:

 Function GetTestClass(lngClassNo as long) as Object Select Case lngClassNo Case 1 Set GetTestClass = New CTest1 Case 2 Set GetTestClass = New CTest2 ... End Select End Function 

If these are your CTest classes defined in the COM library, then you can use the CreateObject statement. You will need to use VB6 to create such a DLL, but you cannot create DLLs in Excel, Access, etc.

 Function GetTestClass(lngClassNo as long) as Object Set GetTestClass = CreateObject("MyDll.CTest" & lngClassNo) End Function 
+4
source share

You can use metaprogramming to do this, although this seems like a hack. Here is an example that uses a couple of helper functions (omitted for brevity):

 Public Function CreateInstance(typeName As String) As Object Dim module As VBComponent Set module = LazilyCreateMPCache() If Not FunctionExists(typeName, module) Then Call AddInstanceCreationHelper(typeName, module) End If Dim instanceCreationHelperName As String instanceCreationHelperName = module.name & ".GetInstanceOf" & typeName Set CreateInstance = Application.Run(instanceCreationHelperName) End Function Sub AddInstanceCreationHelper(typeName As String, module As VBComponent) Dim strCode As String strCode = _ "Public Function GetInstanceOf" & typeName & "() As " & typeName & vbCrLf & _ "Set GetInstanceOf" & typeName & " = New " & typeName & vbCrLf & _ "End Function" Call AddFunction(strCode, module) End Sub 
+3
source share

VB class definitions do define COM interfaces behind the scenes, so you can define data types as defining an abstract interface with specific implementations using implements .

To get any polymorphism, you have to do it, otherwise you will have problems with casting. This is a bit complicated, but it is technically possible to do this using VB. If you want to dig into it find some of VB's advanced books Dan Appleman or Matthew Kurland. I'm not sure they are still being printed, but they are probably available through the Amazon Marketplace.

This works with VB6, and I'm sure it works with VBA.

+2
source share

Hi, I know this is an old thread, but if you still need an answer, look here

http://www.codeproject.com/Articles/164036/Reflection-in-VBA-a-CreateObject-function-for-VBA

He should answer your question.

+1
source share

CallByName function can help you. Suppose your project has several class modules: clsSample0 , clsSample1 and clsSample2 . Add a new class module named clsSpawner , which lists all target classes as public variables with the same name and is declared with the New keyword:

 Public clsSample0 As New clsSample0 Public clsSample1 As New clsSample1 Public clsSample2 As New clsSample2 

Add the Function Spawn() code to the standard module:

 Function Spawn(sClassName) As Object Set Spawn = CallByName(New clsSpawner, sClassName, VbGet) End Function 

Test it with the following code:

 Sub TestSpawn() Dim objSample0a As Object Dim objSample0b As Object Dim objSample1 As Object Dim objSample2 As Object Set objSample0a = Spawn("clsSample0") Set objSample0b = Spawn("clsSample0") Set objSample1 = Spawn("clsSample1") Set objSample2 = Spawn("clsSample2") Debug.Print TypeName(objSample0a) ' clsSample0 Debug.Print TypeName(objSample0b) ' clsSample0 Debug.Print objSample0a Is objSample0b ' False Debug.Print TypeName(objSample1) ' clsSample1 Debug.Print TypeName(objSample2) ' clsSample2 End Sub 

How it works? The Spawn function creates an instance of clsSpawner and calls an instance of clsSpawner to return the requested property, but in fact an instance of clsSpawner creates a new instance of the target class due to the declaration with the keyword New and returns the link.

+1
source share

Perhaps you can do this using a collection class or an array of objects. All objects are in one array.

Your class has a .Name property, and when you create an instance from it, do the following:

 Dim CTest() as New CTest For n = 1 to 10 Redim Preserve CTest(n) CTest(n).Name = "CTest" & CStr(n) Next l 

Quick and dirty. In the above example, 10 CTest objects in one array of objects will be returned. You can also cut the name. And just use CTest (n).

0
source share

All Articles