473,221 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Macro Substitution?

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
4 6871
* "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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Raghuveer Pallikonda | last post by:
Hi, I am trying to stub out debug log messages in my application, if the logging subsystem is not enabled.. For e.g a invocation #define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg so...
8
by: Siemel Naran | last post by:
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__, __LINE__, #x); } MSVC7 gives an error: "error C2014: preprocessor command must start as first nonwhite space".
1
by: me | last post by:
Hi guys I want to insert a load of pieces of data into a map The map has an std::string representing a field name as the key, and the value is a struct with 2 members - the field length and a...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
14
by: Malcolm | last post by:
Hi, I have the following which fails with "disagreement in number of macro arguments" when compiling with Imagecraft ICCAVR. Has anyone got any ideas - its not vital but would make the code a...
1
by: Rodolfo | last post by:
Hello, there's another languages that can do a macro substitution, how can I do this in Csharp. This is an example of what I want to do Dataset ds = new Dataset; string a = "ds"; DataSet...
3
by: Les | last post by:
Is there any way to do something like macro substitution in VB, like in C++ or C or other languages? --------------= Posted using GrabIt =---------------- ------= Binary Usenet downloading...
3
by: XHengDF | last post by:
recent days. i am confuse on the macros in boost. could anyone tell me the replacement rules of the macros in c++, or somelinks about the macros in boost thanks
37
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.