By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

Macro Substitution?

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* "Don" <un*****@oblivion.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
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) else
Dim typeForm As Type = Type.GetType(FORM_NAME_2)
Dim value As Object = Activator.CreateInstance(typeForm)
frmTemp = DirectCast(value, Form) end if
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

"Don" <un*****@oblivion.com> wrote in message
news:6YaYb.531765$X%5.223861@pd7tw2no... 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

Nov 20 '05 #3

P: n/a
Addendum:
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.


I pressed "Send" before completing my reply...

<http://www.google.de/groups?selm=bumk7n%24j9mbf%241%40ID-208219.news.uni-berlin.de>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

P: n/a
Don
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
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.