how many ways are there to create a instance of a class?
-
i hard that there are many ways to to create a instance of a class but i know only dim obj as new MyClass() and i saw one guy wrote Activator.CreateInstance, this could be another way,if so pls tell me when i should use Activator.CreateInstance for creating instance of a class and if there are also other ways then pls tell me how create a instance of a class in different way with sample code. tbhattacharjee
-
i hard that there are many ways to to create a instance of a class but i know only dim obj as new MyClass() and i saw one guy wrote Activator.CreateInstance, this could be another way,if so pls tell me when i should use Activator.CreateInstance for creating instance of a class and if there are also other ways then pls tell me how create a instance of a class in different way with sample code. tbhattacharjee
Activator.CreateInstance has to do with the reflection mechanism, which is used to create classes dynamically (name given at runtime) - something you don't need during the normal course of your program. Just stick to the regular dim way...
-
i hard that there are many ways to to create a instance of a class but i know only dim obj as new MyClass() and i saw one guy wrote Activator.CreateInstance, this could be another way,if so pls tell me when i should use Activator.CreateInstance for creating instance of a class and if there are also other ways then pls tell me how create a instance of a class in different way with sample code. tbhattacharjee
There are a number of ways, but anything other than "new" are in the realms of Reflection, and probably should be avoided unless you have a real need to use them. The web service problem I posted some way down this page uses Activator.CreateInstance. Paul
-
There are a number of ways, but anything other than "new" are in the realms of Reflection, and probably should be avoided unless you have a real need to use them. The web service problem I posted some way down this page uses Activator.CreateInstance. Paul
A class could also be created using a factory pattern. MyClass cls = Factory.CreateMyClass()
-
A class could also be created using a factory pattern. MyClass cls = Factory.CreateMyClass()
Which in turn would have to use either new or reflection, the factory is just a wrapper. Don't confuse matters ;P. Paul
-
Which in turn would have to use either new or reflection, the factory is just a wrapper. Don't confuse matters ;P. Paul
True enough. My point was to say that new and CreateInstance are not the only choices all the time. If you really want to get down to it every method must at some point use new, so there is only one way. ;P The Tao that can be named is not the true Tao.
-
True enough. My point was to say that new and CreateInstance are not the only choices all the time. If you really want to get down to it every method must at some point use new, so there is only one way. ;P The Tao that can be named is not the true Tao.
Mark Nischalke wrote: True enough. My point was to say that new and CreateInstance are not the only choices all the time. Indeed, and don't get me wrong, I'm becoming a great fan of the Factory, particularly since I discovered the joys of unit testing. Mark Nischalke wrote: If you really want to get down to it every method must at some point use new, so there is only one way. Actually, when you put it that way, I wonder whether it's the other way around. Perhaps the keyword
new
translates into something related to the Activator.CreateInstance, much likelock
translates to Monitor.Enter. But I don't know, I try not to consider these things too deeply. :rolleyes: Paul