Constructor Chaining

Can you tell me how to do this in COBOL (C# as the example). I’ve highlighted the constructor chaining method in yellow.

 

public class Car

{

     private Driver driver;

     public Driver Driver

     {

           get { return driver; }

           set { driver = value; }

     }

 

     public Car()

           : this (new Driver())

     {

           Driver.Name = "Speed Racer";

     }

          

     public Car(Driver driver)

     {

           this.driver = driver

     }

}

 

public class Driver

{

     private string _name;

     public string Name

     {

           get { return _name; }

           set { _name = value; }

     }

}

 

  • Something like:

          class-id Car.

          01 Driver type Driver property.

          method-id new.

              invoke self::new(new Driver)

              set Driver::Name to "Speed Racer"

          end method.

          method-id new (driver as type Driver)

              set self::Driver to driver

          end method.

          end class.

          class-id Driver.

          01 #Name string property.

          end class.

    The 'property' keyword on the 01 level instance fields sets up a private field and a public property.  The # in '#Name' serves to allow the use of a COBOL reserved word (Name) as a data name.  The '#' character is removed from the externalized name.

  • Unfortunately, the INVOKE SELF::NEW must be the first statement in the procedure division of the subsequent NEW method.  If fact, it cannot be inside a TRY...CATCH block.

  • Hi hleerobinson, that's correct, but I'm not sure I understand why this is 'unfortunate'...

  • Since it cannot be in the TRY..CATCH block, the lack of error handling is obviously unfortunate. Forcing it to be the first statement in the method means I cannot do any calculation or processing of the parameters in NEW(B) before calling the NEW(A) method. That means the logic must be performed before the call to NEW(B). This defeats the entire idea of modular programming. If I call NEW(B) twenty-five times in my code then I have to have twenty-five copies of whatever logic I wanted to put into NEW(B).
  • I see your point, but this is intrinsic to the design of  .NET.  Chained constructors must be called as the first operation in a constructor, and this applies to C# and any other language implemented on .NET.   It is in fact a restriction in the underlying IL.

    I think the idea behind this is that, until the chained constructor has successfully completed, the newly constructed object is an unstable state, and therefore the ability to bail out of the chained constructor in a try/catch/finally block is not very useful.  Other people may be able to comment further on the design principle here.

    I would say that, in general, those parameters that will be passed on to the chained constructor will be validated within that chained constructor, and any additional parameters can be validated following the call to the chained constructor.