PDA

View Full Version : Reference between different units



Neuromancer
13-12-2007, 11:36 PM
Hello,
I am developping a strategy game using Lazarus. In a first unit called Map, I defined the classes TProvince et TSquare (a square belongs to a province). In the Economy unit, I defined the following classes for the economic objects in a province or in a square : TProvinceEconomy, TSquareEconomy, TActivitySector (an industry in a square), Tware (a ware available in a province).

Classes which represent industries (e.g. Farming) inherit from TActivitySector. Classes which represent wares (e.g. wheat), inherit from Tware. Each industry produce uses several wares to produce a ware, and therefore should be associated with the TWare object. An industry belongs to a square economy. A ware belongs to a province economy.

I also defined a few abstract classes and interfaces in another unit to help with circular references. The Map unit has a reference on the Economy unit in its implementation part.

I hope my explaination is clear.

Map unit :

Unit Map
TProvince = class
FEconomy : TAbstractProvinceEconomy; // A province economy
FSquare : array of TSquare; // The squares which belong to the province
constructor Create;
end;

TSquare = class
FProvince : TProvince; // The province the square belongs to.
FEconomy : TAbstractSquareEconomy; // A square economy
constructor Create(AProvince : TProvince );
end;

constructor TProvince.Create;
begin
self.FEconomy := TProvinceEconomy.Create;
SetLength(self.FSquares, 2);
self.FSquares[0] := TSquare.Create(self);
end;
When creating a TSquare object, I give the province as a parameter, and I create a TSquareEconomy.

Economy unit :

Unit Economy
TWare = class(TAbstractWare)
FName : string;
end;

TProvinceEconomy = class(TAbstractProvinceEconomy)
FWares : array of TAbstractWare; // Liste of available wares
constructor Create;
end;

TSquareEconomy = class(TAbstractSquareEconomy)
FSectors : array of TActivitySector; // List of industries
end;

// Secteur d'activit?©
TActivitySector = class
FName : string;
// The ware produced by the industry
FWare : TWare;
constructor Create;
end;

// Secteur Agriculture (inherits from TActivitySector)
TFarming = class(TActivitySector)
constructor create;
end;

TWheat = class(TWare)
end;

constructor TProvinceEconomy.Create;
begin
SetLength(self.FWares, 2);
// The ware "wheat" is available.
self.FWares[0] := TWheat .Create;
end;

constructor TFarming.Create;
begin
// The ware we want to associate with the industry.
self.FWare := self.FEconomy.FSquare.FProvince.FEconomy.FWares[0] as TWheat;
end;

But it doesn't work. FWares[0] seems to be empty. So I don't know if I am using the right way. Could you help me ?

Robert Kosek
14-12-2007, 12:08 AM
Uh... you forgot many important things like: end., implementation, interface, type.

Do you know how to write a unit? I'm asking because if what you gave is a snippet, you could at least mark where you snip so we know it's a real unit.

Firstoff, this isn't a managed language, so you do have to reference other units. Like so:
unit unit1;

interface

uses unit2;

implementation

end.
unit unit2;

interface

uses unit1;

implementation

end.

Otherwise this'd be a problem for you. ;)
// Secteur d'activit?©
TActivitySector = class
FName : string;
// The ware produced by the industry
TWare &#58; TWare; // <-- You assign to FWare, not TWare. ;&#41;
constructor Create;
end;

marcov
30-12-2007, 09:44 PM
Firstoff, this isn't a managed language

I don't entirely see what that has to do with this issue?!?

Robert Kosek
30-12-2007, 11:53 PM
Firstoff, this isn't a managed language, so you do have to reference other units.
unit unit1;

interface

uses unit2; // <------------------------------------------------

implementation

end.
unit unit2;

interface

uses unit1; // <------------------------------------------------

implementation

end.

In a managed language anything in the same assembly is referenced automatically, thus no need for external references or inclusions because it's internal. You don't have this. You must manually reference EVERY UNIT you change, reference, or need FOR EVERY UNIT that you write.

cairnswm
31-12-2007, 01:03 PM
If it compiled the references are there. Its just been trimmed for the sake of being brief.

However too much has been trimmed to see where the problem could be. In the classes listed I cannot see where TFarming inherits a TEconomy from.

I would suggest making a zip file available somewhere so that people could download it and try and trace the problem.