Create a new object using the text name of the class

后端 未结 5 1595
刺人心
刺人心 2020-12-06 07:00

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

I will have a library of classes, and depending on some other var

5条回答
  •  攒了一身酷
    2020-12-06 07:15

    There's no reflection in VBA, so I don't think this is possible. You'd have to 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
    

    Unless that is your CTest classes are defined in a COM DLL, in which case you could use the CreateObject statement. You would need to use VB6 to create such a DLL though, you can't create DLLs in Excel, Access, etc.

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

提交回复
热议问题