Highlighted
New Member.
703 views

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

     }

}

 

0 Likes
1 Reply
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Constructor Chaining

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.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.