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. Best Method to Instantiate a Class

Best Method to Instantiate a Class

Scheduled Pinned Locked Moved ASP.NET
discussion
3 Posts 2 Posters 0 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.
  • B Offline
    B Offline
    Bardy85
    wrote on last edited by
    #1

    I have three different ways to Instatiate a class and fill the properties. Even though all three ways work I'm not sure which would be the best to use in the form "Best Practices" Method 1 Simple instantiate class, set property and call select to fill object

    Foo NewFoo = new Foo();

    NewFoo.ID = 10;
    NewFoo.Select();

    Method 2 In constructor Set Id and then call Select to Fill Object

    Foo NewFoo = new Foo(10).Select();

    Method 3 In the construtor I use the ID and automatically call the Select to fill the Object

    Foo NewFoo = new Foo(10);

    All three are basically the same. If anyone does it differently or better. Please let me know what your thoughts are. Thanks.

    J 1 Reply Last reply
    0
    • B Bardy85

      I have three different ways to Instatiate a class and fill the properties. Even though all three ways work I'm not sure which would be the best to use in the form "Best Practices" Method 1 Simple instantiate class, set property and call select to fill object

      Foo NewFoo = new Foo();

      NewFoo.ID = 10;
      NewFoo.Select();

      Method 2 In constructor Set Id and then call Select to Fill Object

      Foo NewFoo = new Foo(10).Select();

      Method 3 In the construtor I use the ID and automatically call the Select to fill the Object

      Foo NewFoo = new Foo(10);

      All three are basically the same. If anyone does it differently or better. Please let me know what your thoughts are. Thanks.

      J Offline
      J Offline
      jc net
      wrote on last edited by
      #2

      I would use Method 4:) Foo NewFoo = new Foo(10); NewFoo.Select();

      goto Tech-check

      B 1 Reply Last reply
      0
      • J jc net

        I would use Method 4:) Foo NewFoo = new Foo(10); NewFoo.Select();

        goto Tech-check

        B Offline
        B Offline
        Bardy85
        wrote on last edited by
        #3

        So basically Method 2. I was kinda going with that. Thanks.

        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