You are on page 1of 5

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

Dim strClassName as String


strClassName = "CTest" & CStr(lngClassNo)
Set GetTestClass = New instance of class(strClassName)
End Function

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

answered Jun 29 '09 at 10:54


shareimprove this answer
Patrick McDonald
25k95989
I think you might run into casting issues if you tried to return an object. You
would have to do something with a COM interface.
ConcernedOfTunbridgeWells Jun 29 '09 at 10:57
Oh well. I started of with a Select Case as above, but was hoping for something
cleaner....not the end of the world. Thanks MT. Jun 29 '09 at 11:01
VBA allows late binding, so there are no casting issues as far as I can see, you
2 just need to be careful to avoid runtime errors as the compiler won't pick up any
typos in method names etc. Patrick McDonald Jun 29 '09 at 11:12
These are the only two ways to pull off what you're suggesting. The interface
suggestion from Concerned would simplify some of the implementation issues (I
use interfaces frequently for this very purpose) so you can retain compile-time
1
type checking in a polymorphic manner. However, you can still use runtime
binding on the function calls with using an Object instance. Joel Goodwin Jun
29 '09 at 11:17
Thanks for this. It's nice to be being able to use the interface. Just a shame about
having to use the select case... MT. Jun 29 '09 at 12:02
add a comment

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

through Amazon Marketplace.


This works with VB6 and I'm fairly sure it works with VBA.
answered Jun 29 '09 at 11:03
shareimprove this answer
ConcernedOfTunbridgeWells
40.8k1197165
Very nice. It does work thanks. Nice to be able to use the compiler and not have
to worry about typos etc. MT. Jun 29 '09 at 11:56
Implements just forces you implement all the public methods/properties of a
given class. How would that help with creating a class by name? Oorang Jun
29 '09 at 16:01
Creating an object is a no brainer. The problem is not creating the class but using
it subequently. You can use Object and late binding and hope for the best.
1
Implements gives you type-safe polymorphism. ConcernedOfTunbridgeWells
Jun 29 '09 at 16:12
add a comment
up vote You can use metaprogramming to do this, although it does seem like quite a hack.
2 down Here is an example that uses a couple of helper functions (omitted for brevity):
vote
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

shareimprove this answer answered Jan 28 '11 at 16:16

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

answered Jul 3 '09 at 8:54

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

You might also like