There are four different ways of reading resources from a table: read all table resources, read all resources starting past a resource type and number, read an specific resource by specifying resource type and number, and read a resource by specifying its name.
Read all resource starting with the first resource by calling method TCtResource.First() method and then read the remaining resources by calling TCtResource.Next(). To read the first resource in a table, call the following method:
function TCtResource.First(lock : boolean) : boolean; virtual;
TCtResource.First() method retrieve the first resource stored in a table. Parameter lock is used to indicate if the resource should be locked, if it is found. TCTResource.First() return true if the resource was found or false if no resource was found. If an error is detected, TCtResource.First() throws an ECtError exception.
To read all other resources of a table call the following TCtResource method:
function Next(lock : boolean) : boolean; virtual;
TCtResource.Next() retrieve the next resource stored in a table, after a successful call to TCtResource.First() is made. lock is used to indicate if the resource should be locked, if it is found. TCtResource.Next() returns true if the resource was found or false if no resource was found. If an error is detected, TCtResource.First() throws an ECtError exception.
Example
procedure DisplayAllResources(hTable : TCtTable);
var
hRes : TCTResource;
begin
Writeln('Displaying all resources of table ', hTable.Table);
hRes := TCtResource.Create(hTable, 1, 2, 'test');
try
// get the first resource
if hRes.First(false) then
begin
repeat
// display resource type, number and name
Writeln('Resource type: ', hRes.TypeID,
', number: ', hRes.Number,
', name: ', hRes.Resource);
until not hRes.Next(false);
end;
except
on err: ECtError do Writeln('Error ', err.Message);
end;
hRes.Free;
end;
Read all resources starting from a given resource type and number by calling method TCtResource.Next(). Start by instantiating a resource object setting the resource type and a resource number and then call TCtResource.Next(). In this case a resource is found if its type is greater or equal to the specified type and its number is greater than the specified number.
Example
procedure DisplayResources(hTable : TCtTable);
var
hRes : TCTResource;
begin
Writeln('Displaying all resources of table ', hTable.Table);
hRes := TCtResource.Create(hTable,$10001, 0, 'MyResource');
try
// get the resources resource
while hRes.Next(false) do
begin
// display resource type, number and name
Writeln('Resource type: ', hRes.TypeID,
', number: ', hRes.Number,
', name: ', hRes.Resource);
end;
except
on err: ECtError do Writeln('Error ', err.Message);
end;
hRes.Free;
end;
Read a specific resource by providing exact resource type and number by calling TCtResource.Find():
function Find(TypeID : LongWord; number : LongWord; lock : boolean) : boolean; overload; virtual;
Locate and retrieve a resource in a table based on type and number. TypeID is a value of the resource type and number is the value of the resource number to be located. lock is used to indicate if the resource should be locked, if it is found. TCtResource.Find() return true if the resource is found or false if this particular resource was not found. If an error occurs, TCtResource.Find() throws an ECtError exception.
Example
procedure ShowResource(hTable : TCtTable);
var
hRes : TCTResource;
begin
Writeln('Displaying a resource of table ', hTable.Table);
hRes := TCtResource.Create(hTable, 0, 0);
try
// find a resource
if hRes.Find(1, 2, false) then
begin
// display resource type, number and name
Writeln('Resource type: ', hRes.TypeID,
', number: ', hRes.Number,
', name: ', hRes.Resource);
end;
except
on err: ECtError do Writeln('Error ', err.Message);
end;
hRes.Free;
end;
Read a resource by providing its name by calling TCtResource.Find():
function Find(name : string; lock : boolean) : boolean; overload; virtual;
TCtResource.Find() locates and retrieves a resource by name. c-tree Plus can not guarantee unique resource names. name is the resource name being located and lock indicates if the resource should locked, if it is found. TCtResource.Find() returns true if the resource is located or false if no resource is located. If errors are detected a ECtError exception object is thrown.
Example
procedure FindResource(hTable : TCtTable);
var
hRes : TCTResource;
begin
Writeln('Finding resource of table ', hTable.Table);
hRes := TCtResource.Create(hTable, 0, 0);
try
// find a resource
if hRes.Find('MyResource', false) then
begin
// display resource type, number and name
Writeln('Resource type: ', hRes.TypeID,
', number: ', hRes.Number,
', name: ', hRes.Resource);
end;
except
on err: ECtError do Writeln('Error ', err.Message);
end;
hRes.Free;
end;