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 = "frmThisFor m"
Const FORM_NAME_2 as String = "frmThatFor m"
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 4 6909
* "Don" <un*****@oblivi on.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>
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.Activato r.CreateInstanc e method, which allows you
to dynamically load & create an object based on a string.
Something like:
Const FORM_NAME_1 as String = "frmThisFor m" Const FORM_NAME_2 as String = "frmThatFor m"
Dim frmTemp As Form If condition then
Dim typeForm As Type = Type.GetType(FO RM_NAME_1)
Dim value As Object = Activator.Creat eInstance(typeF orm)
frmTemp = DirectCast(valu e, Form) else
Dim typeForm As Type = Type.GetType(FO RM_NAME_2)
Dim value As Object = Activator.Creat eInstance(typeF orm)
frmTemp = DirectCast(valu e, Form) end if
Note for Type.GetType to work above, "frmThisFor m" needs to be in the
current assembly. If "frmThisFor m" is not in the current assembly, then
FORM_NAME_1 needs to be in the "namespace.clas s, assembly" instead of simply
the "class" format.
Hope this helps
Jay
"Don" <un*****@oblivi on.com> wrote in message
news:6YaYb.5317 65$X%5.223861@p d7tw2no... 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 = "frmThisFor m" Const FORM_NAME_2 as String = "frmThatFor m"
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
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=bum k7n%24j9mbf%241 %40ID-208219.news.uni-berlin.de>
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
I figured out how to do what I wanted:
Private Sub MacroSubstution Example(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.My ClassName1"
Const CLASS_NAME_2 as String = "MyNameSpace.My ClassName2"
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.Creat eInstance(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.Messa ge)
End Try
End Sub
This worked for me. Thanks for the tips, guys!
- Don This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 that
LOGMSG("Log Me\n");
will be a NO-OP if the Logger::Enabled() returns false, else the
|
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".
|
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 bool
indicating whether the field is a special field or not.
The struct representing the value has a constructor that takes a single
parameter for the length, and defaults the boolean to false.
|
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.
Execution is faster. Where will it be stotred?(Is it in bss/stack/?)
FUNCTION:
|
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 lot nicer to read:
#define SET(x,y) (x |=(1<<y))
#define PIEZO PORTB,5
| |
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 ds2 = &a;
|
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 made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-
|
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
|
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 inline function ?
thanks for any help in advance ...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |