Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how many ways are there to create a instance of a class?

how many ways are there to create a instance of a class?

Scheduled Pinned Locked Moved ASP.NET
question
7 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    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

    P P 2 Replies Last reply
    0
    • T Tridip Bhattacharjee

      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

      P Offline
      P Offline
      Plunging_Falcon
      wrote on last edited by
      #2

      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...

      1 Reply Last reply
      0
      • T Tridip Bhattacharjee

        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

        P Offline
        P Offline
        Paul Riley
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • P Paul Riley

          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

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          A class could also be created using a factory pattern. MyClass cls = Factory.CreateMyClass()

          P 1 Reply Last reply
          0
          • N Not Active

            A class could also be created using a factory pattern. MyClass cls = Factory.CreateMyClass()

            P Offline
            P Offline
            Paul Riley
            wrote on last edited by
            #5

            Which in turn would have to use either new or reflection, the factory is just a wrapper. Don't confuse matters ;P. Paul

            N 1 Reply Last reply
            0
            • P Paul Riley

              Which in turn would have to use either new or reflection, the factory is just a wrapper. Don't confuse matters ;P. Paul

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              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.

              P 1 Reply Last reply
              0
              • N Not Active

                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.

                P Offline
                P Offline
                Paul Riley
                wrote on last edited by
                #7

                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 like lock translates to Monitor.Enter. But I don't know, I try not to consider these things too deeply. :rolleyes: Paul

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups