473,473 Members | 4,297 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hwo do I instrantiate a object

Hi,
This might sound like a unusual question, but I would like to be able to
read a string from a XML stream and use this string at runtime to be able to
instantiate a 'new' object.

For example:
dim strObj as string = "ObjectA" 'This will be read from XML file
dim obj as new ObjectA 'Next time it could be ObjectB so I need to 'new
ObjectB'

Any help will be great.
Thanks in advance.

Regards.
Nov 22 '05 #1
4 1198
Sparmar2000 <Sp*********@discussions.microsoft.com> wrote:
This might sound like a unusual question, but I would like to be able to
read a string from a XML stream and use this string at runtime to be able to
instantiate a 'new' object.

For example:
dim strObj as string = "ObjectA" 'This will be read from XML file
dim obj as new ObjectA 'Next time it could be ObjectB so I need to 'new
ObjectB'


Have a look at Activator.CreateInstance and Type.GetType. You could
also use an XmlSerializer to make life easier for yourself :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Jon,
Thaks a lot for your quick response. I am familier with CreateInstance, and
also GetType, but can you please point me in the right direction with
XMLSerializer - perhaps with an example.

Thanks in advance.
Regards
Suresh

P.S. I will look on the web for example and papers on the subject as well as
soon as I have finished this post.
"Jon Skeet [C# MVP]" wrote:
Sparmar2000 <Sp*********@discussions.microsoft.com> wrote:
This might sound like a unusual question, but I would like to be able to
read a string from a XML stream and use this string at runtime to be able to
instantiate a 'new' object.

For example:
dim strObj as string = "ObjectA" 'This will be read from XML file
dim obj as new ObjectA 'Next time it could be ObjectB so I need to 'new
ObjectB'


Have a look at Activator.CreateInstance and Type.GetType. You could
also use an XmlSerializer to make life easier for yourself :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #3
Sparmar2000 <Sp*********@discussions.microsoft.com> wrote:
Thaks a lot for your quick response. I am familier with CreateInstance, and
also GetType, but can you please point me in the right direction with
XMLSerializer - perhaps with an example.
Well, the best thing to do is look in MSDN for an example. To be
honest, I hadn't used XmlSerializer before last week, but it seems to
be pretty easy to do. If you look at the examples in the XmlSerializer
constructor page, and the Serialize/Deserialize methods, it should make
it reasonably clear. Let me know if there's anything in particular you
don't understand.
P.S. I will look on the web for example and papers on the subject as well as
soon as I have finished this post.


Goodo :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #4
Jon,
Just had a look at examples after searching the web - and you are right, it
seems pretty stright forward and clear.

Thanks a lot for your help.
Regards.
Suresh....

"Jon Skeet [C# MVP]" wrote:
Sparmar2000 <Sp*********@discussions.microsoft.com> wrote:
Thaks a lot for your quick response. I am familier with CreateInstance, and
also GetType, but can you please point me in the right direction with
XMLSerializer - perhaps with an example.


Well, the best thing to do is look in MSDN for an example. To be
honest, I hadn't used XmlSerializer before last week, but it seems to
be pretty easy to do. If you look at the examples in the XmlSerializer
constructor page, and the Serialize/Deserialize methods, it should make
it reasonably clear. Let me know if there's anything in particular you
don't understand.
P.S. I will look on the web for example and papers on the subject as well as
soon as I have finished this post.


Goodo :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #5

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

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Nick Zdunic | last post by:
I have a remotable object running in my host application. The host starts up and creates the object. Within a method to start the remote object doing its thing it creates an object. ...
4
by: Sparmar2000 | last post by:
Hi, This might sound like a unusual question, but I would like to be able to read a string from a XML stream and use this string at runtime to be able to instantiate a 'new' object. For...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
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,...
1
isladogs
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.