E2149 Class does not have a default property (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi)

You have used a class instance variable in an array expression, but the class type has not declared a default array property.


program Produce;

  type
    Base = class
    end;

  var
    b : Base;

  procedure P;
    var ch : Char;
  begin
    ch := b[1];
  end;

begin
end.

The example above elicits an error because 'Base' does not declare an array property, and 'b' is not an array itself.


program Solve;

  type
    Base = class
      function GetChar(i : Integer) : Char;
      property data[i : Integer] : Char read GetChar; default;
    end;

  var
    b : Base;

  function Base.GetChar(i : Integer) : Char;
  begin GetChar := 'A';
  end;

  procedure P;
    var ch : Char;
  begin
    ch := b[1];
    ch := b.data[1];
  end;

begin
end.

When you have declared a default property for a class, you can use the class instance variable in array expression, as if the class instance variable itself were actually an array. Alternatively, you can use the name of the property as the actual array accessor.

Note: If you have hints turned on, you will receive two warnings about the value assigned to 'ch' never being used.