Previous Topic Next topic Print topic


Structures, Value Types - in COBOL and C# and VB.NET

COBOL uses the valuetype-id to define a value type.

C# uses struct to define a value type.

C# COBOL VB.NET
struct StudentRecord
{
  public string name;
  public float gpa;

  public StudentRecord(string name, float gpa)
  {
    this.name = name;
    this.gpa = gpa;
  }
}
StudentRecord stu = new StudentRecord("Bob", 3.5f);
StudentRecord stu2 = stu;

stu2.name = "Sue";
Console.WriteLine(stu.name);    // Prints Bob
Console.WriteLine(stu2.name);   // Prints Sue
valuetype-id StudentRecord.
01 #name            string public.
01 averageMark      float-short public.

method-id new(studentName as string, averageMark as float-short).
    set self::name to studentName
    set self::averageMark to averageMark
end method.

end valuetype.

class-id RecordClient.

method-id main static.
    declare stu = new StudentRecord("Bob", 3.5).
    declare stu2 as type StudentRecord
    set stu2 to stu
    set stu2::name to "Sue"
    display stu::name   *> Prints Bob
    display stu2::name  *> Prints Sue
end method.

end class.
Structure StudentRecord
  Public name As String
  Public gpa As Single

  Public Sub New(ByVal name As String, ByVal gpa As Single)
    Me.name = name
    Me.gpa = gpa
  End Sub
End Structure

Dim stu As StudentRecord = New StudentRecord("Bob", 3.5)
Dim stu2 As StudentRecord = stu

stu2.name = "Sue"
Console.WriteLine(stu.name)    ' Prints Bob
Console.WriteLine(stu2.name)   ' Prints Sue
Previous Topic Next topic Print topic