Constructors calling one another ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    #1

    Constructors calling one another ...

    A class can have more than one constructor, but can't one constructor call
    another? It appears that the code below bombs and the error message claims
    .... "denotes a 'class' which is not valid in the given context".

    Thanks in advance! - Mark

    public class cWhatever
    {
    public cWhatever(Strin g str1)
    {
    whatever(str1, string.Empty);
    }
    public cWhatever(Strin g str1, String str2)
    {
    //Do something
    }
    }


  • Michael Culley

    #2
    Re: Constructors calling one another ...

    The other alternative is to put the code into a seperate function and call that from both constructors. That was you can execute the
    initialization code wherever you like in the constructor.

    --
    Michael Culley


    Comment

    • Keith Patrick

      #3
      Re: Constructors calling one another ...

      Just my $.02, but I prefer the former method. Reduces the paths of code to
      1 and means less changes to make if all paths ultimately lead to a single
      ctor.

      "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
      news:uDvIOb0cDH A.828@TK2MSFTNG P11.phx.gbl...[color=blue]
      > The other alternative is to put the code into a seperate function and call[/color]
      that from both constructors. That was you can execute the[color=blue]
      > initialization code wherever you like in the constructor.
      >
      > --
      > Michael Culley
      >
      >[/color]


      Comment

      • Jeff Louie

        #4
        Re: Constructors calling one another ...

        Mark.... You can chain constructors or use static factory methods.

        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


        Regards,
        Jeff

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • William Stacey

          #5
          Re: Constructors calling one another ...

          However if you use a private method, then the paths lead there as well
          (assuming your not doing anything else in the constructor.) Even if you
          use "this", you can still have other code in the constructors that gets
          executed after the "this" so there is no gaurentee of only one path, only
          that some common code will get excecuted. So it's probably about the same
          either way? Not advocating either way, just poking for knowledge myself.

          --
          William Stacey, DNS MVP

          "Keith Patrick" <richard_keith_ patrick@nospamh otmail.com> wrote in message
          news:ecCB$11cDH A.1884@TK2MSFTN GP10.phx.gbl...[color=blue]
          > Just my $.02, but I prefer the former method. Reduces the paths of code[/color]
          to[color=blue]
          > 1 and means less changes to make if all paths ultimately lead to a single
          > ctor.
          >
          > "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
          > news:uDvIOb0cDH A.828@TK2MSFTNG P11.phx.gbl...[color=green]
          > > The other alternative is to put the code into a seperate function and[/color][/color]
          call[color=blue]
          > that from both constructors. That was you can execute the[color=green]
          > > initialization code wherever you like in the constructor.
          > >
          > > --
          > > Michael Culley
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Michael Culley

            #6
            Re: Constructors calling one another ...

            But if you want the initialisation code run at the end of the constructor you've got no choice.

            --
            Michael Culley


            "Keith Patrick" <richard_keith_ patrick@nospamh otmail.com> wrote in message news:ecCB$11cDH A.1884@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Just my $.02, but I prefer the former method. Reduces the paths of code to
            > 1 and means less changes to make if all paths ultimately lead to a single
            > ctor.
            >
            > "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
            > news:uDvIOb0cDH A.828@TK2MSFTNG P11.phx.gbl...[color=green]
            > > The other alternative is to put the code into a seperate function and call[/color]
            > that from both constructors. That was you can execute the[color=green]
            > > initialization code wherever you like in the constructor.
            > >
            > > --
            > > Michael Culley
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Keith Patrick

              #7
              Re: Constructors calling one another ...

              Which is why all of my "other" ctors have no code in them; they are merely
              overloads in lieu of default parameter support in the language.

              "William Stacey" <staceyw@mvps.o rg> wrote in message
              news:%239cLx71c DHA.2176@TK2MSF TNGP09.phx.gbl. ..[color=blue]
              > However if you use a private method, then the paths lead there as well
              > (assuming your not doing anything else in the constructor.) Even if you
              > use "this", you can still have other code in the constructors that gets
              > executed after the "this" so there is no gaurentee of only one path, only
              > that some common code will get excecuted. So it's probably about the same
              > either way? Not advocating either way, just poking for knowledge myself.
              >
              > --
              > William Stacey, DNS MVP
              >
              > "Keith Patrick" <richard_keith_ patrick@nospamh otmail.com> wrote in message
              > news:ecCB$11cDH A.1884@TK2MSFTN GP10.phx.gbl...[color=green]
              > > Just my $.02, but I prefer the former method. Reduces the paths of code[/color]
              > to[color=green]
              > > 1 and means less changes to make if all paths ultimately lead to a[/color][/color]
              single[color=blue][color=green]
              > > ctor.
              > >
              > > "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
              > > news:uDvIOb0cDH A.828@TK2MSFTNG P11.phx.gbl...[color=darkred]
              > > > The other alternative is to put the code into a seperate function and[/color][/color]
              > call[color=green]
              > > that from both constructors. That was you can execute the[color=darkred]
              > > > initialization code wherever you like in the constructor.
              > > >
              > > > --
              > > > Michael Culley
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Jon Skeet

                #8
                Re: Constructors calling one another ...

                Mark <field027_nospa m_@umn.edu> wrote:[color=blue]
                > A class can have more than one constructor, but can't one constructor call
                > another? It appears that the code below bombs and the error message claims
                > ... "denotes a 'class' which is not valid in the given context".[/color]

                <snip>

                In addition to the other responses, you might want to read
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • William Stacey

                  #9
                  Re: Constructors calling one another ...

                  That is nice if you can do it. Sometimes that is not possible or you have
                  to make the one ctor overly complex to handle all the different parms.

                  --
                  William Stacey, DNS MVP

                  "Keith Patrick" <richard_keith_ patrick@nospamh otmail.com> wrote in message
                  news:u8bK3u2cDH A.4020@tk2msftn gp13.phx.gbl...[color=blue]
                  > Which is why all of my "other" ctors have no code in them; they are[/color]
                  merely[color=blue]
                  > overloads in lieu of default parameter support in the language.
                  >
                  > "William Stacey" <staceyw@mvps.o rg> wrote in message
                  > news:%239cLx71c DHA.2176@TK2MSF TNGP09.phx.gbl. ..[color=green]
                  > > However if you use a private method, then the paths lead there as well
                  > > (assuming your not doing anything else in the constructor.) Even if[/color][/color]
                  you[color=blue][color=green]
                  > > use "this", you can still have other code in the constructors that gets
                  > > executed after the "this" so there is no gaurentee of only one path,[/color][/color]
                  only[color=blue][color=green]
                  > > that some common code will get excecuted. So it's probably about the[/color][/color]
                  same[color=blue][color=green]
                  > > either way? Not advocating either way, just poking for knowledge[/color][/color]
                  myself.[color=blue][color=green]
                  > >
                  > > --
                  > > William Stacey, DNS MVP
                  > >
                  > > "Keith Patrick" <richard_keith_ patrick@nospamh otmail.com> wrote in[/color][/color]
                  message[color=blue][color=green]
                  > > news:ecCB$11cDH A.1884@TK2MSFTN GP10.phx.gbl...[color=darkred]
                  > > > Just my $.02, but I prefer the former method. Reduces the paths of[/color][/color][/color]
                  code[color=blue][color=green]
                  > > to[color=darkred]
                  > > > 1 and means less changes to make if all paths ultimately lead to a[/color][/color]
                  > single[color=green][color=darkred]
                  > > > ctor.
                  > > >
                  > > > "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
                  > > > news:uDvIOb0cDH A.828@TK2MSFTNG P11.phx.gbl...
                  > > > > The other alternative is to put the code into a seperate function[/color][/color][/color]
                  and[color=blue][color=green]
                  > > call[color=darkred]
                  > > > that from both constructors. That was you can execute the
                  > > > > initialization code wherever you like in the constructor.
                  > > > >
                  > > > > --
                  > > > > Michael Culley
                  > > > >
                  > > > >
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • William Stacey

                    #10
                    Re: Constructors calling one another ...

                    Thanks Jon. Always good info.

                    --
                    William Stacey, DNS MVP

                    "Jon Skeet" <skeet@pobox.co m> wrote in message
                    news:MPG.19c232 c289384d5998a4b 2@news.microsof t.com...[color=blue]
                    > Mark <field027_nospa m_@umn.edu> wrote:[color=green]
                    > > A class can have more than one constructor, but can't one constructor[/color][/color]
                    call[color=blue][color=green]
                    > > another? It appears that the code below bombs and the error message[/color][/color]
                    claims[color=blue][color=green]
                    > > ... "denotes a 'class' which is not valid in the given context".[/color]
                    >
                    > <snip>
                    >
                    > In addition to the other responses, you might want to read
                    > http://www.pobox.com/~skeet/csharp/constructors.html
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet/
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • Scott C. Wagner

                      #11
                      Re: Constructors calling one another ...

                      You cannot initialize readonly class members in a private function.
                      Those can only be assigned to in a ctor. Therefore, in some cases, it
                      is important that you redirect to another ctor instead of a private
                      function.

                      Scott Wagner
                      Seisint, Inc.

                      William Stacey wrote:[color=blue]
                      > However if you use a private method, then the paths lead there as well
                      > (assuming your not doing anything else in the constructor.) Even if you
                      > use "this", you can still have other code in the constructors that gets
                      > executed after the "this" so there is no gaurentee of only one path, only
                      > that some common code will get excecuted. So it's probably about the same
                      > either way? Not advocating either way, just poking for knowledge myself.
                      >[/color]

                      Comment

                      • Keith Patrick

                        #12
                        Re: Constructors calling one another ...

                        Why? If all your ctors call a master one, and the only copy of the init
                        code is there, it will run at the end of the construction process.

                        "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
                        news:uwaX6b2cDH A.1696@TK2MSFTN GP10.phx.gbl...[color=blue]
                        > But if you want the initialisation code run at the end of the constructor[/color]
                        you've got no choice.[color=blue]
                        >
                        > --
                        > Michael Culley
                        >
                        >
                        > "Keith Patrick" <richard_keith_ patrick@nospamh otmail.com> wrote in message[/color]
                        news:ecCB$11cDH A.1884@TK2MSFTN GP10.phx.gbl...[color=blue][color=green]
                        > > Just my $.02, but I prefer the former method. Reduces the paths of code[/color][/color]
                        to[color=blue][color=green]
                        > > 1 and means less changes to make if all paths ultimately lead to a[/color][/color]
                        single[color=blue][color=green]
                        > > ctor.
                        > >
                        > > "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
                        > > news:uDvIOb0cDH A.828@TK2MSFTNG P11.phx.gbl...[color=darkred]
                        > > > The other alternative is to put the code into a seperate function and[/color][/color][/color]
                        call[color=blue][color=green]
                        > > that from both constructors. That was you can execute the[color=darkred]
                        > > > initialization code wherever you like in the constructor.
                        > > >
                        > > > --
                        > > > Michael Culley
                        > > >
                        > > >[/color]
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        Working...