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

Calling constructors

Scheduled Pinned Locked Moved C#
questioncsharp
4 Posts 4 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.
  • P Offline
    P Offline
    pjholliday
    wrote on last edited by
    #1

    I have a form with three constructors each with a different signiture. i.e. public Form_1() ... public Form_1(string Name) ... public Form_1(string Name, int Number) ... How can i call the first constructor from the second and call the second from the third? At the minute I have to copy the common code into functions and call the functions but would prefer to call the constructors as i used to do in VB.Net. Thanks

    S 1 Reply Last reply
    0
    • P pjholliday

      I have a form with three constructors each with a different signiture. i.e. public Form_1() ... public Form_1(string Name) ... public Form_1(string Name, int Number) ... How can i call the first constructor from the second and call the second from the third? At the minute I have to copy the common code into functions and call the functions but would prefer to call the constructors as i used to do in VB.Net. Thanks

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Use the 'this' keyword like this public Form_1() : this(null) { //blah blah } public Form_1(string Name) : this(Name, 0) { } public Form_1(string Name, int Number) { }

      H 1 Reply Last reply
      0
      • S S Senthil Kumar

        Use the 'this' keyword like this public Form_1() : this(null) { //blah blah } public Form_1(string Name) : this(Name, 0) { } public Form_1(string Name, int Number) { }

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        You've generated an extra call for no good reason. A better example would be:

        public Form1() : this(null, 0)
        {
        }
        public Form1(string name) : this(name, 0)
        {
        }
        public Form1(string name, int number)
        {
        // Your implementation goes here.
        }

        Why generate an extra call on the stack just to "redirect" to another method? This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        D 1 Reply Last reply
        0
        • H Heath Stewart

          You've generated an extra call for no good reason. A better example would be:

          public Form1() : this(null, 0)
          {
          }
          public Form1(string name) : this(name, 0)
          {
          }
          public Form1(string name, int number)
          {
          // Your implementation goes here.
          }

          Why generate an extra call on the stack just to "redirect" to another method? This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          Heath Stewart wrote: Why generate an extra call on the stack just to "redirect" to another method? To avoid duplicating defaults and/or code (which could lead to inconsistent behavior). Actually, if you Google for it you'll notice that this is one of those religious things. Personally, I agree with you, with the added annoyance that single-stepping through code like this is really boring. Yes, even I am blogging now!

          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