Constructor

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

    Constructor

    Hi,

    see my classes:

    public class One
    {
    public One(string s) { .... }
    }

    public class Two:One
    {
    public class Two(int x)
    {
    ...
    }
    }

    what if I have to make some preparation before call the "base"
    constructor from Two? for example:

    public class Two(int x)
    {
    string s = String.Format(" Your int was {0}",x);
    base(s); // this is not working !!!!
    }

    So, the question is: how can I call the base constructor if
    I can't call using the ":base( )" syntax. Is there any other
    possibility to call the base constructor INSIDE the descendant
    class' constructor?

    aroan


  • Daniel O'Connell

    #2
    Re: Constructor


    "Zoltan Hernyak" <aroan@ektf.h u> wrote in message
    news:es9iov4lq3 4an1m571rhb1jmm sgcq1ihio@4ax.c om...[color=blue]
    > Hi,
    >
    > see my classes:
    >
    > public class One
    > {
    > public One(string s) { .... }
    > }
    >
    > public class Two:One
    > {
    > public class Two(int x)
    > {
    > ...
    > }
    > }
    >
    > what if I have to make some preparation before call the "base"
    > constructor from Two? for example:
    >
    > public class Two(int x)
    > {
    > string s = String.Format(" Your int was {0}",x);
    > base(s); // this is not working !!!!
    > }
    >
    > So, the question is: how can I call the base constructor if
    > I can't call using the ":base( )" syntax. Is there any other
    > possibility to call the base constructor INSIDE the descendant
    > class' constructor?
    >[/color]
    No, the language does not allow this.
    Assuming you have a simple case like the one you posed, you can do
    public Two(int x) : base(String.For mat("Your int was {0}",x) {

    }

    If the case is more complex, then you may be able to use a static method (be
    careful not to cause a stack overflow like this):

    public class Two : One
    {
    public Two(int x) : base(Two.Format Int(x))
    {

    }
    static string FormatInt(int x)
    {
    //make sure you NEVER call new Two() from thsi method, however!
    return String.Format(" Your int was {0}",x);
    }
    }[color=blue]
    > aroan
    >
    >[/color]


    Comment

    • Zoltan Hernyak

      #3
      Re: Constructor

      On Sun, 12 Oct 2003 10:15:58 GMT, in
      microsoft.publi c.dotnet.langua ges.csharp you wrote:
      [color=blue]
      > public Two(int x) : base(Two.Format Int(x))[/color]

      Ok. I understand. But let's suppose One(int a, int b) constuctor needs
      two ints, and works only when a<b.

      One x = new One(1,2); // will work
      One x = new One(2,1); // throws an exception
      One x = new One(1,1); // throws an exception

      I want to extend this class to "TWO". Two class' constructor
      can work when a<b or a=b. When a<b, we needs to change a with b,
      when a=b, we can't allow the instance to be created.

      public Two(int a, int b):base( ???? )
      {
      if (a<b) call base(a,b)
      else if (a<b) call base(b,a);
      else throw new Exception("Wron g attribues!")
      }

      Two x = new Two(1,2); // will work
      Two x = new Two(2,1); // will work
      Two x = new Two(1,1); // throws an exception

      How can I do that?
      [color=blue][color=green]
      >> aroan[/color][/color]
      On Sun, 12 Oct 2003 10:15:58 GMT, in
      microsoft.publi c.dotnet.langua ges.csharp you wrote:
      [color=blue]
      > public Two(int x) : base(Two.Format Int(x))[/color]

      Ok. I understand. But let's suppose One(int a, int b) constuctor needs
      two ints, and works only when a<b.

      One x = new One(1,2); // will work
      One x = new One(2,1); // throws an exception
      One x = new One(1,1); // throws an exception

      I want to extend this class to "TWO". Two class' constructor
      can work when a<b or a=b. When a<b, we needs to change a with b,
      when a=b, we can't allow the instance to be created.

      public Two(int a, int b):base( ???? )
      {
      if (a<b) call base(a,b)
      else if (a<b) call base(b,a);
      else throw new Exception("Wron g attribues!")
      }

      Two x = new Two(1,2); // will work
      Two x = new Two(2,1); // will work
      Two x = new Two(1,1); // throws an exception

      How can I do that?
      [color=blue][color=green]
      >> aroan[/color][/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Constructor

        Zoltan Hernyak <aroan@ektf.h u> wrote:[color=blue][color=green]
        > > public Two(int x) : base(Two.Format Int(x))[/color]
        >
        > Ok. I understand. But let's suppose One(int a, int b) constuctor needs
        > two ints, and works only when a<b.
        >
        > One x = new One(1,2); // will work
        > One x = new One(2,1); // throws an exception
        > One x = new One(1,1); // throws an exception
        >
        > I want to extend this class to "TWO". Two class' constructor
        > can work when a<b or a=b. When a<b, we needs to change a with b,
        > when a=b, we can't allow the instance to be created.[/color]

        Well, there *are* ways round it - the best is probably to use a factory
        method instead, ie:

        public static Two CreateTwo (int a, int b)
        {
        if (a < b)
        return new Two (a, b);
        if (b < a)
        return new Two (b, a);
        if (a==b)
        throw new ArgumentExcepti on ("a and b must be different");
        }

        private Two (int a, int b) : base (a, b)
        {
        }

        I can't say I've run into this kind of problem many times though.

        --
        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

        • Daniel O'Connell

          #5
          Re: Constructor


          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.19f365 64f04be37e98986 1@msnews.micros oft.com...[color=blue]
          > Zoltan Hernyak <aroan@ektf.h u> wrote:[color=green][color=darkred]
          > > > public Two(int x) : base(Two.Format Int(x))[/color]
          > >
          > > Ok. I understand. But let's suppose One(int a, int b) constuctor needs
          > > two ints, and works only when a<b.
          > >
          > > One x = new One(1,2); // will work
          > > One x = new One(2,1); // throws an exception
          > > One x = new One(1,1); // throws an exception
          > >
          > > I want to extend this class to "TWO". Two class' constructor
          > > can work when a<b or a=b. When a<b, we needs to change a with b,
          > > when a=b, we can't allow the instance to be created.[/color]
          >
          > Well, there *are* ways round it - the best is probably to use a factory
          > method instead, ie:
          >
          > public static Two CreateTwo (int a, int b)
          > {
          > if (a < b)
          > return new Two (a, b);
          > if (b < a)
          > return new Two (b, a);
          > if (a==b)
          > throw new ArgumentExcepti on ("a and b must be different");
          > }
          >
          > private Two (int a, int b) : base (a, b)
          > {
          > }
          >
          > I can't say I've run into this kind of problem many times though.
          >[/color]
          Ya, me either..
          Goes to show, however, always provide an accurate sample case or people may
          not understand what you need.[color=blue]
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Zoltan Hernyak

            #6
            Re: Constructor

            On Sun, 12 Oct 2003 14:20:12 +0100, Jon Skeet [C# MVP]
            <skeet@pobox.co m> wrote:
            [color=blue]
            >public static Two CreateTwo (int a, int b)
            >{
            > if (a < b)
            > return new Two (a, b);
            > if (b < a)
            > return new Two (b, a);
            > if (a==b)
            > throw new ArgumentExcepti on ("a and b must be different");
            >}[/color]

            Ahh, I see. I worked a lot in Delphi, where constructors behave like
            methods, and it is simple to call a base constructor (and many
            constructors) inside another constructor... I was just thinking
            the old way... thanks for the advices.

            Zoltan Hernyak

            Comment

            Working...