Show: Delphi C++
Display Preferences

System.Assigned

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

function Assigned(var P): Boolean;

Properties

Type Visibility Source Unit Parent
function public System.pas System System

Description

Tests for a nil (unassigned) pointer or procedural variable.

Use Assigned to determine whether the pointer or the procedure referenced by P is nil. P must be a variable reference of a pointer or procedural type.

Assigned(P) corresponds to the test P <> nil for a pointer variable, and @P <> nil for a procedural variable.

Assigned returns False if P is nil, True otherwise.

Tip: When testing object events and procedures for assignment, you cannot test for nil, and using Assigned is the right way.
var
  LNotifyEvent: TNotifyEvent;

begin
  if LNotifyEvent <> nil then    { This is not OK and generates compiler error. }
    Writeln('something');

  if Assigned(LNotifyEvent) then { This is OK. }
    Writeln('something');
end;

Tip: In particular cases there is a difference between using Assigned(Something) and Something <> nil. For instance, in the following code Assigned(LMyFunction) checks whether the LMyFunction variable is actually assigned, while LMyFunction<> nil tests the result value of LMyFunction for assignment.

function MyFunction: Pointer;
begin
  Result := nil;
end;

type
  TMyFunction = function: Pointer;

var
  LMyFunction: TMyFunction = MyFunction;

begin
  Writeln('Statement "LMyFunction <> nil" evaluates to: ', LMyFunction <> nil);
  Writeln('Statement "Assigned(LMyFunction)" evaluates to: ', Assigned(LMyFunction));
end.

Outputs:

Statement "LMyFunction <> nil" evaluates to: FALSE
Statement "Assigned(LMyFunction)" evaluates to: TRUE
Note: Assigned cannot detect a dangling pointer--that is, one that is not nil, but that no longer points to valid data. For example, in the code example for Assigned (SystemAssigned), Assigned does not detect that P is not valid.

See Also

Code Examples

Personal tools
In Other Languages
Previous Versions
Assistance