473,396 Members | 1,784 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,396 software developers and data experts.

Creating framework objects from class name strings

Hi all,

If I have the name in a string of one of the framework's core classes (such
as "System.Collections.ArrayList"), what's the easiest way for me to create
an instance of the appropriate class?

In VB6 I would have simply used CreateObject, is there an equally simple way
in VB.NET?

Thanks,

--

(O)enone
Nov 21 '05 #1
4 2023
"Oenone" <oe****@nowhere.com> schrieb:
If I have the name in a string of one of the framework's core classes
(such as "System.Collections.ArrayList"), what's the easiest way for me to
create an instance of the appropriate class?


'Activator.CreateInstance'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
> 'Activator.CreateInstance'.

Thanks Herfried, got this working fine as follows:

\\\
Dim list As IList
Dim collName As String

collName = "System.Collections.ArrayList"
list =
DirectCast(Activator.CreateInstance(System.Type.Ge tType(collName)), IList)
///

However, when I set collName to "Microsoft.VisualBasic.Collection" (to get
one of the old VB6-style collections), System.Type.GetType() returns
Nothing. Do you know why?

I'm trying to write some code that knows how to deal with all different
sorts of collections, and it's handling everything except for
Microsoft.VisualBasic.Collection objects just fine. It'd be a shame to have
to put a separate If clause in to specifically create these objects...

Thanks,

--

(O)enone
Nov 21 '05 #3
"Oenone" <oe****@nowhere.com> schrieb:
\\\
Dim list As IList
Dim collName As String

collName = "System.Collections.ArrayList"
list =
DirectCast(Activator.CreateInstance(System.Type.Ge tType(collName)), IList)
///

However, when I set collName to "Microsoft.VisualBasic.Collection" (to get
one of the old VB6-style collections), System.Type.GetType() returns
Nothing. Do you know why?


It doesn't know which assembly the type is defined in. You'll have to know
the assembly which contains the type:

\\\
Dim o As Object
For Each a As [Assembly] In AppDomain.CurrentDomain.GetAssemblies()
If a.FullName.StartsWith("Microsoft.VisualBasic,") Then
o = a.CreateInstance("Microsoft.VisualBasic.Collection ")
Exit For
End If
Next a
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
Just curious as to where one would need to create a Framework object and how
it would be used...Thanks as I'm just learning!
--
Dennis in Houston
"Oenone" wrote:
'Activator.CreateInstance'.


Thanks Herfried, got this working fine as follows:

\\\
Dim list As IList
Dim collName As String

collName = "System.Collections.ArrayList"
list =
DirectCast(Activator.CreateInstance(System.Type.Ge tType(collName)), IList)
///

However, when I set collName to "Microsoft.VisualBasic.Collection" (to get
one of the old VB6-style collections), System.Type.GetType() returns
Nothing. Do you know why?

I'm trying to write some code that knows how to deal with all different
sorts of collections, and it's handling everything except for
Microsoft.VisualBasic.Collection objects just fine. It'd be a shame to have
to put a separate If clause in to specifically create these objects...

Thanks,

--

(O)enone

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gonzalo briceno | last post by:
I have been using phplib for a while and I really like the framework except for form creation. Maybe it is me but I in my opinion there isn't a good way to create forms or should I say, everything...
23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
19
by: Steve | last post by:
Can anyone point me to a primer for creating OCX controls in VB .net? In particular, I want to access a web service in a VB6 application (not an easy thing to do). So, if I can write an ActiveX...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
3
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that...
5
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
3
dmjpro
by: dmjpro | last post by:
plz send me a good link which can clearify me how the J2EE framework works i want the details information .... plz help thanx
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

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.