Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#2

AW: bidirektionale Assoziationen - wie in c# ?

  Alt 25. Dez 2015, 09:08
Eine Unit ist ein kompletter Namespace. Es gibt aber noch einen Unterschied:
Delphi-Quellcode:
type
  TFoo = class
    Bar: TBar; // Problem, weil TBar noch nicht bekannt ist
  end;

  TBar = class
    Foo: TFoo; // kein Problem
  end;
Das wäre in C# so kein Problem, hier bekommen wir einen Fehler.

Die Lösung:
Delphi-Quellcode:
type
  // Forward Declarations
  TBar = class;
  TFoo = class; // wäre hier nicht notwendig

// type <- nicht erlaubt

  // Real Declarations
  TFoo = class
    Bar: TBar; // kein Problem mehr
  end;

// type <- nicht erlaubt

  TBar = class
    Foo: TFoo; // kein Problem
  end;

type // wieder erlaubt
  TFooBar = class
  end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat