Search This Blog

Thursday, July 13, 2023

TMyObject = object !!😲

Yes sir!

There was an old object in TurPascal that was called `Object`!

In Turbo Pascal, the Object type is a base object type that does not require explicit memory management like classes.
You can create and use objects of this type without the need to explicitly free them. 
Here's a sample code demonstrating the usage of the Object type in Turbo Pascal:


program ObjectDemo;

type
  TMyObject = object
    Value: Integer;
    procedure Initialize(AValue: Integer);
    procedure Display;
  end;

procedure TMyObject.Initialize(AValue: Integer);
begin
  Value := AValue;
end;

procedure TMyObject.Display;
begin
  Writeln('Value:', Value);
end;

var
  MyObject: TMyObject;

begin
  MyObject.Initialize(42);
  MyObject.Display;

  // No need to free the object explicitly

  Readln;
end.



In this code, we define a type `TMyObject` using the `object` keyword. It has a member variable `Value` of type `Integer` and two methods: `Initialize` and `Display`.

The Initialize method sets the value of `Value` based on the provided parameter. The `Display` method prints the value of `Value` to the console.

Inside the `begin` and `end` block, we declare a variable `MyObject` of type `TMyObject`. We then call the `Initialize` method on `MyObject` to set its value to 42 and call the `Display` method to print the value to the console.

Since `TMyObject` is an object, it does not require explicit memory management. The object is automatically allocated and deallocated when it goes out of scope.

Note that the `object` type in Turbo Pascal does not support inheritance or virtual methods like classes do. It is a simpler construct primarily used for encapsulating data and methods within a single unit of code.

Is it available yet in modern Delphi?

Yes, there is, in the matter of backward compatibility.

No comments:

Post a Comment