Macro Substitution?

  • Thread starter Thread starter Don
  • Start date Start date
D

Don

I think "macro substitution" is the correct term for what I want to do, but,
to be sure, here is a description of what I'd like to know is possible:

I want to be able to create a create an object of a type whose name is
stored in a constant. For example:


Const FORM_NAME_1 as String = "frmThisForm"
Const FORM_NAME_2 as String = "frmThatForm"

Dim frmTemp as Form


If condition then
frmTemp = CType(frmTemp, FORM_NAME_1)
frmTemp = New FORM_NAME_1
else
frmTemp = CType(frmTemp, FORM_NAME_2)
frmTemp = New FORM_NAME_2
end if

frmTemp.Show

etc....


In the pseudo code above, I want to somehow access the string stored in
FORM_NAME_1 and FORM_NAME_2 in the lines of code where I use CType and New.
Is it possible to even do this in VB.NET?

- Don
 
* "Don said:
I think "macro substitution" is the correct term for what I want to do, but,
to be sure, here is a description of what I'd like to know is possible:

I want to be able to create a create an object of a type whose name is
stored in a constant. For example:

That's not possible with VB.NET.
 
Don,
I think "macro substitution" is the correct term for what I want to do, but,
to be sure, here is a description of what I'd like to know is possible:
I normally reserve "Macro substitution" for the ability replace text
dynamically during C++ compilation. A feature that VB.NET does not support.
In the pseudo code above, I want to somehow access the string stored in
FORM_NAME_1 and FORM_NAME_2 in the lines of code where I use CType and New.
Is it possible to even do this in VB.NET?
Not with CType & New.

Have a look at the System.Activator.CreateInstance method, which allows you
to dynamically load & create an object based on a string.

Something like:
Const FORM_NAME_1 as String = "frmThisForm"
Const FORM_NAME_2 as String = "frmThatForm" Dim frmTemp As Form
If condition then
Dim typeForm As Type = Type.GetType(FORM_NAME_1)
Dim value As Object = Activator.CreateInstance(typeForm)
frmTemp = DirectCast(value, Form)

Dim typeForm As Type = Type.GetType(FORM_NAME_2)
Dim value As Object = Activator.CreateInstance(typeForm)
frmTemp = DirectCast(value, Form)

Note for Type.GetType to work above, "frmThisForm" needs to be in the
current assembly. If "frmThisForm" is not in the current assembly, then
FORM_NAME_1 needs to be in the "namespace.class, assembly" instead of simply
the "class" format.

Hope this helps
Jay
 
I figured out how to do what I wanted:


Private Sub MacroSubstutionExample(ByVal blnUseClass1 as Boolean)

' These are constants containing the names of my classes (the names must
include the namespace)
Const CLASS_NAME_1 as String = "MyNameSpace.MyClassName1"
Const CLASS_NAME_2 as String = "MyNameSpace.MyClassName2"

Dim strType As String ' I'll throw the name of the class I want in
here.
Dim obj As clsBase ' This is the object with which I will be
instantiation either MyClassName1 or MyClassName1
' NOTE: In my example, clsBase is the parent
class of MyClassName1 and MyClassName1. Using
' Dim obj As Object would work just as well.

Try

' Determine which class I should instantiate
If blnUseClass1 Then
strType = CLASS_NAME_1
Else
strType = CLASS_NAME_2
End If

' Create new object of the type that we want (** This is it!!! **)
obj = Activator.CreateInstance(Type.GetType(strType))

' Show a message which displays the type of the object I just
instantiated
MsgBox(TypeName(obj))

' Clean up
obj = Nothing

Catch ex As Exception

' Display error message
MsgBox(ex.Message)

End Try

End Sub


This worked for me. Thanks for the tips, guys!

- Don
 
Back
Top