VBA Create A New Object Using The Text Name of The Class
VBA Create A New Object Using The Text Name of The Class
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 variable, I
want to get one of these classes at runtime.
E.g. I have "CTest1", "CTest2", "CTest3"
I would have function similar to the below
Function GetTestClass(lngClassNo as long) as Object
up vote 2 down
vote favorite
1
vba excel-vba
edited Jul 4 '13 at asked Jun 29 '09 at
14:17 10:45
shareimprove this
question
Meehow MT.
20.3k114566 1113720
add a comment
6 Answers
active oldest votes
up vote There's no reflection in VBA, so I don't think this is possible. You'd have to do
3 down something like the following I'm afraid:
vote
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
VB class definitions are really defining COM interfaces behind the scenes, so one can
define data types as an abstract interface definition with concrete implementations
using the implements keyword.
up vote
2 down
To get any sort of polymorphism you have to do this, otherwise you will have
vote
problems with casting. It is somewhat fiddly but technically possible to do this with
VB. If you want to dig into it find some of the advanced VB books by Dan Appleman
or Matthew Kurland. I'm not sure if they're still in print but they're probably available
Jordan
31633
add a comment
You might be able to do it with a collection class or object array. All the objects
are in one array.
In your class have a .Name property and when you create an instance of it do this:
Dim CTest() as New CTest
For n = 1 to 10
Redim Preserve CTest(n)
CTest(n).Name = "CTest" & CStr(n)
Next l
up vote 0
down vote Quick and dirty. The above example would return 10 CTest objects in a single
object array. You could also ditch the .Name and just use CTest(n).
answered Jul 1 '09 at 18:32
shareimprove this answer
mandroid
82731228
add a comment
You've lost me a bit with this: how can polymorphism help you to create instances
of a class using just a string?
up vote 0 I want to be able to look up in a table which class has to be used with a selected
down vote option. So how do you do this without using a select-statement?
shareimprove this answer
Are you asking for the motiviation behind being able to do this? MT. Jul 3
'09 at 13:47
No more like how this is done Anonymous Jul 3 '09 at 14:34
I don't think it can be in VBA as reflection is not supported. (Although I
would love to be shot down on this and am not an expert in it). MT. Jul 4
'09 at 6:41
add a comment
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-CreateObjectup vote function-for-VBA
0 down
It should answer your question.
vote
shareimprove this answer answered Jan 8 '14 at 15:04
Paul D
1
Hey Paul D, welcome to Stack Overflow and thanks for adding an answer. Just a
tip - it's always better for future reference to include the body of the solution
inside your answer. That way, if the link ever breaks or moves, your answer will
still be helpful. Thanks!! yochannah Jan 8 '14 at 15:26