0

So at first, I had the following constructor.

public WaitPanelThread(Point origin,
                       int delay,
        //bool westEast,
                       String direction,
                       Panel panel,
                       Color colour,
                       Semaphore semaphore,
                       Semaphore semaphore2,
                       Semaphore parkSemaphore,
                       Semaphore nextSlot,
                       Buffer buffer,
                       Buffer buffer2,
                       Buffer parkBuffer,
                       String panelParkSpot,
                       String nextSpot)
    {
        this.origin = origin;
        this.delay = delay;
        this.nextSpot = nextSpot;
        this.parkBuffer = parkBuffer;
        this.nextSlot = nextSlot;
        this.panelParkSpot = panelParkSpot;
        //this.westEast = westEast;
        this.direction = direction;
        this.parkSemaphore = parkSemaphore;

        this.panel = panel;
        this.colour = colour;
        this.plane = origin;
        if (direction == "down")
        {
            this.yDelta = 2;
            this.xDelta = 0;
        }
        if (direction == "left")
        {
            this.xDelta = -10;
            this.yDelta = 0;
        }
        if (direction == "right")
        {
            this.xDelta = 10;
            this.yDelta = 0;
        }
        this.panel.Paint += new PaintEventHandler(this.panel_Paint);
        //this.xDelta = westEast ? +10 : -10;
        //this.yDelta = 0;
        this.semaphore = semaphore;
        this.semaphore2 = semaphore2;
        this.buffer = buffer;
        this.buffer2 = buffer2;

    }

And then I realized, for some objects I would need three more parameters. So I overloaded the constructor with three more parameters, copy-pasted the code and assigned the extra parameters as well.

 public WaitPanelThread(Point origin,
                       int delay,
        //bool westEast,
                       String direction,
                       Panel panel,
                       Color colour,
                       Semaphore semaphore,
                       Semaphore semaphore2,
                       Semaphore parkSemaphore,
                       Semaphore nextSlot,
                       Buffer buffer,
                       Buffer buffer2,
                       Buffer parkBuffer,
                       String panelParkSpot,
                       String nextSpot,
                       bool isTurn,
                       Semaphore altSem,
                       Buffer altBuf)
    {
        this.origin = origin;
        this.delay = delay;
        this.nextSpot = nextSpot;
        this.parkBuffer = parkBuffer;
        this.nextSlot = nextSlot;
        this.panelParkSpot = panelParkSpot;
        //this.westEast = westEast;
        this.direction = direction;
        this.parkSemaphore = parkSemaphore;
        this.isTurn = isTurn;

        this.panel = panel;
        this.colour = colour;
        this.plane = origin;
        this.altSem = altSem;
        this.altBuf = altBuf;
        if (direction == "down")
        {
            this.yDelta = 2;
            this.xDelta = 0;
        }
        if (direction == "left")
        {
            this.xDelta = -10;
            this.yDelta = 0;
        }
        if (direction == "right")
        {
            this.xDelta = 10;
            this.yDelta = 0;
        }
        this.panel.Paint += new PaintEventHandler(this.panel_Paint);
        //this.xDelta = westEast ? +10 : -10;
        //this.yDelta = 0;
        this.semaphore = semaphore;
        this.semaphore2 = semaphore2;
        this.buffer = buffer;
        this.buffer2 = buffer2;

    }

So as you can see, The two constructors are mostly identical in its implementation except for the three extra parameters (One boolean, one semaphore and one buffer.) What I want to know is, instead of writing all the code in the overloaded constructor, is there a way to reference the first constructor and only have to write extra code for the extra parameters?

I'm talking about something like using the "Super()" method in an inherited class (I looked into it, can't be done here because they are within the same class).

Thank you.

Kando
  • 3
  • 4

1 Answers1

0

The syntax for that is (in the constructor):

public WaitPanelThread( ... parameters...) : this(..parameters for another constructor..)
{
     // initialize the optional parameters here
}
Maximilian Gerhardt
  • 5,188
  • 3
  • 28
  • 61
  • Thank you so much. I think that is exactly what I need. So when I use this, the first constructor will run assigning the values as usual, and then my new parameters will be assigned as well? Also, how will the order be when initializing an object be? The same order as the first constructor follow by the new order specified? Thank you ! – Kando Sep 11 '15 at 17:36
  • Right, the first call will be to the constructor specified with the `this(...)` call, and then, the part `//initialize the optional parameters here` will be executed. The `this(..)` part of the constructor is always called first. Read up on the topic of *constructor overloading* if there are more questions. – Maximilian Gerhardt Sep 11 '15 at 19:34
  • Thank you, You've been extremely helpful. – Kando Sep 12 '15 at 06:35