calling base and this constructor at same time, is possible?

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

    calling base and this constructor at same time, is possible?

    i want to call base and this constructor at the same time. is is possible?

    i mean:
    B(int a, int b, int c):base(a):this (b)
    {
    // do something with c
    }

    at java i used to do that:

    B(int a, int b, int c)
    {
    super (a);
    this(b);
    // do something with c
    }

    and if yes, how is its syntax.

    thanks in advance

    alpos



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: calling base and this constructor at same time, is possible?

    Murat,

    No, this is not possible. If you want to call the this and base
    constructor, then you will have to call "this" constructor which in turn
    calls the "base" constructor (in a chain).

    In your case, you would have to have B(int, int, int) call B(int) which
    in turn would call base(int).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "murat oguzalp" <murat_oguzla p> wrote in message
    news:%23j2p53Sh EHA.2620@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    >i want to call base and this constructor at the same time. is is possible?
    >
    > i mean:
    > B(int a, int b, int c):base(a):this (b)
    > {
    > // do something with c
    > }
    >
    > at java i used to do that:
    >
    > B(int a, int b, int c)
    > {
    > super (a);
    > this(b);
    > // do something with c
    > }
    >
    > and if yes, how is its syntax.
    >
    > thanks in advance
    >
    > alpos
    >
    >
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: calling base and this constructor at same time, is possible?

      Rakesh,

      This is not possible, as you need to indicate whether or not you call
      "this" or "base" before any code in the constructor is run.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Rakesh Rajan" <RakeshRajan@di scussions.micro soft.com> wrote in message
      news:8319BAE3-3FAC-46E7-BF24-75D637E1C9D4@mi crosoft.com...[color=blue]
      >I don't know if i got ur question right. But you could use the base keyword
      > for this operation.
      >
      > Eg:
      > B(int a, int b, int c)
      > {
      > base(a);
      > this.<construct or>(b);
      > // do something with c
      > }
      >
      >
      > "murat oguzalp" wrote:
      >[color=green]
      >> i want to call base and this constructor at the same time. is is
      >> possible?
      >>
      >> i mean:
      >> B(int a, int b, int c):base(a):this (b)
      >> {
      >> // do something with c
      >> }
      >>
      >> at java i used to do that:
      >>
      >> B(int a, int b, int c)
      >> {
      >> super (a);
      >> this(b);
      >> // do something with c
      >> }
      >>
      >> and if yes, how is its syntax.
      >>
      >> thanks in advance
      >>
      >> alpos
      >>
      >>
      >>
      >>[/color][/color]


      Comment

      • Mike Schilling

        #4
        Re: calling base and this constructor at same time, is possible?


        "murat oguzalp" <murat_oguzla p> wrote in message
        news:%23j2p53Sh EHA.2620@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        >i want to call base and this constructor at the same time. is is possible?
        >
        > i mean:
        > B(int a, int b, int c):base(a):this (b)
        > {
        > // do something with c
        > }
        >
        > at java i used to do that:
        >
        > B(int a, int b, int c)
        > {
        > super (a);
        > this(b);
        > // do something with c
        > }
        >[/color]

        No you didn't; that is not legal java.

        In Java, you'd do something like:

        B(int a, int b, int c)
        {
        super(a);
        init(b);
        }

        B(int b)
        {
        init(b);
        }

        private init(int b)
        {
        ... common init logic
        }

        In C#, you do the same.


        Comment

        • murat oguzalp

          #5
          Re: calling base and this constructor at same time, is possible?

          thaks to everyone for sharing information.
          Mike is right. i thought, i could have done it.
          but neither java, it's not legitimate
          alpos
          "Mike Schilling" <mscottschillin g@hotmail.com> wrote in message
          news:eNrFhNUhEH A.2916@TK2MSFTN GP12.phx.gbl...[color=blue]
          >
          > "murat oguzalp" <murat_oguzla p> wrote in message
          > news:%23j2p53Sh EHA.2620@TK2MSF TNGP10.phx.gbl. ..[color=green]
          > >i want to call base and this constructor at the same time. is is[/color][/color]
          possible?[color=blue][color=green]
          > >
          > > i mean:
          > > B(int a, int b, int c):base(a):this (b)
          > > {
          > > // do something with c
          > > }
          > >
          > > at java i used to do that:
          > >
          > > B(int a, int b, int c)
          > > {
          > > super (a);
          > > this(b);
          > > // do something with c
          > > }
          > >[/color]
          >
          > No you didn't; that is not legal java.
          >
          > In Java, you'd do something like:
          >
          > B(int a, int b, int c)
          > {
          > super(a);
          > init(b);
          > }
          >
          > B(int b)
          > {
          > init(b);
          > }
          >
          > private init(int b)
          > {
          > ... common init logic
          > }
          >
          > In C#, you do the same.
          >
          >[/color]


          Comment

          Working...