HomeDelphiDelphi Error – E2149 Class does not have a default property

Delphi Error – E2149 Class does not have a default property

Delphi Compiler Error

E2149 Class does not have a default property

Reason for the Error & Solution

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.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...