473,322 Members | 1,494 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,322 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 6885
* "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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.