Correct me if I am wrong here

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

Guest

I am under the assumption that in vb.net the followin

Dim objClass as Object = Activator.CreateInstance(Type
Dim myClass as MyTestClass = DirectCast(objClass,MyTestClass

would be the equivalent o

Dim myClass as MyTestClass = New MyTestClas

Or do i need to add the following to CreateInstanc

Dim objClass as Object = Activator.CreateInstance(Type, New Object()
Dim myClass as MyTestClass = DirectCast(objClass,MyTestClass

Thank
 
Hi Joe,

Maybe I do not understand your question but to create an object from a class

Dim myClass as New MyTestClass

But correct me if I understand you wrong?

Cor
 
* =?Utf-8?B?Sm9l?= said:
I am under the assumption that in vb.net the following

Dim objClass as Object = Activator.CreateInstance(Type)
Dim myClass as MyTestClass = DirectCast(objClass,MyTestClass)

would be the equivalent of

Dim myClass as MyTestClass = New MyTestClass

I doubt that this is equivalent. Why not use the 2nd method?
Or do i need to add the following to CreateInstance

If the code above works, no.
 
Herfried K. Wagner said:
I doubt that this is equivalent. Why not use the 2nd method?

What are you talking about? Activator.CreateInstance calls the constructor
(in this case the default ctor ()) and returns the instance. How is this
different than the second??
 
* "CJ Taylor said:
What are you talking about? Activator.CreateInstance calls the constructor
(in this case the default ctor ()) and returns the instance. How is this
different than the second??

The result is the same. Nevertheless, have a look at the IL using
"ILDASM.EXE".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top