Hwo do I instrantiate a object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Sparmar2000 said:
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,
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.
 
Sparmar2000 said:
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,
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....
 
Back
Top