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. General Programming
  3. C#
  4. constructor calling constructor

constructor calling constructor

Scheduled Pinned Locked Moved C#
question
7 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.
  • S Offline
    S Offline
    SimonS
    wrote on last edited by
    #1

    Scenario: I have an object that has 2 overloads of a constructor. For some reason, overload1 in turn calls overload2. i.e. I want to defer object creation until overload2 is eventually called. I'm not sure of the syntax, as constructors don't seem to be treated as normal functions. Can someone provide me with code for this? Cheers, Simon "Every good work of software starts by scratching a developer's personal itch.", Eric S. Raymond

    N 1 Reply Last reply
    0
    • S SimonS

      Scenario: I have an object that has 2 overloads of a constructor. For some reason, overload1 in turn calls overload2. i.e. I want to defer object creation until overload2 is eventually called. I'm not sure of the syntax, as constructors don't seem to be treated as normal functions. Can someone provide me with code for this? Cheers, Simon "Every good work of software starts by scratching a developer's personal itch.", Eric S. Raymond

      N Offline
      N Offline
      Neil Van Note
      wrote on last edited by
      #2

      class Foo
      {
      int value;
      public Foo()
      : this(0)
      {
      }
      public Foo(int i)
      {
      value = i;
      }
      }

      S 1 Reply Last reply
      0
      • N Neil Van Note

        class Foo
        {
        int value;
        public Foo()
        : this(0)
        {
        }
        public Foo(int i)
        {
        value = i;
        }
        }

        S Offline
        S Offline
        SimonS
        wrote on last edited by
        #3

        Thanks, Neil. I'll pass this on to my team. Is this a good practice (constructor calling constructor)? Cheers, Simon "Every good work of software starts by scratching a developer's personal itch.", Eric S. Raymond

        N 1 Reply Last reply
        0
        • S SimonS

          Thanks, Neil. I'll pass this on to my team. Is this a good practice (constructor calling constructor)? Cheers, Simon "Every good work of software starts by scratching a developer's personal itch.", Eric S. Raymond

          N Offline
          N Offline
          Neil Van Note
          wrote on last edited by
          #4

          It’s more personal preference issue, although I don’t see how it could be a bad practice and in this case it is an integral part of the language. Code reuse (outside of cut and paste) is never a bad practice in my opinion. Regards

          S 1 Reply Last reply
          0
          • N Neil Van Note

            It’s more personal preference issue, although I don’t see how it could be a bad practice and in this case it is an integral part of the language. Code reuse (outside of cut and paste) is never a bad practice in my opinion. Regards

            S Offline
            S Offline
            SimonS
            wrote on last edited by
            #5

            Is there no additional overhead, in terms of MSIL code, to pull this off? What about the alternative of creating an additional function that had the "shared" code that both constructors could use? In your opinion, wouldn't this produce cleaner code? Cheers, Simon "Every good work of software starts by scratching a developer's personal itch.", Eric S. Raymond

            N 2 Replies Last reply
            0
            • S SimonS

              Is there no additional overhead, in terms of MSIL code, to pull this off? What about the alternative of creating an additional function that had the "shared" code that both constructors could use? In your opinion, wouldn't this produce cleaner code? Cheers, Simon "Every good work of software starts by scratching a developer's personal itch.", Eric S. Raymond

              N Offline
              N Offline
              Neil Van Note
              wrote on last edited by
              #6

              Again, its personal preference. In overhead terms, there really is nothing to pull off; the initial constructor does generate a call to the target constructor. If you were going to supply a common private initialization member, it may accomplish the same thing with or without parameter passing. If your initialization member is parameterized, I would venture a guess that it carries the same amount of overhead. Regards

              1 Reply Last reply
              0
              • S SimonS

                Is there no additional overhead, in terms of MSIL code, to pull this off? What about the alternative of creating an additional function that had the "shared" code that both constructors could use? In your opinion, wouldn't this produce cleaner code? Cheers, Simon "Every good work of software starts by scratching a developer's personal itch.", Eric S. Raymond

                N Offline
                N Offline
                Neil Van Note
                wrote on last edited by
                #7

                In terms of cleaner code, if the class carries a significant amount of initialization that is common across all constructors, yes a shared initialization member can be cleaner. I have also seen cases where these initialization members also become a reset mechanism for the class. Not that this is a bad thing, but in large projects and/or as time goes on and maintenance coding kicks in, it may be easier for a member of your team to forget what the primary purpose of the member was and start calling virtual members within it, which is a bad thing. My two cents. Regards

                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