wallyg wrote:Thank you.
I have not done anything like that before, so I will go back to the FB manual and try to work out how that works. Is there any other documents that go into more detail than the current standard FB manual?
I'll say that the Manual is the best place to look for this info. That, and this forum of course =D
This is a simple approach to your problem (if I understood your requirement correctly):
Code: Select all
/'
The interface for a record in the car table
Any object deriving from this interface can be added to
the car table
'/
type ICarRecord extends Object
declare virtual destructor()
declare abstract property brand() as string
declare abstract property year() as string
declare abstract property color() as string
declare abstract property model() as string
end type
destructor ICarRecord()
end destructor
/'
A concrete implementation of the ICarRecord interface
This defines a generic 'Car' object with several properties.
'/
type Car extends ICarRecord
public:
declare constructor( _
byref as const string, _
byref as const string, _
byref as const string, _
byref as const string )
declare destructor() override
declare property brand() as string override
declare property model() as string override
declare property year() as string override
declare property color() as string override
private:
declare constructor()
m_brand as string
m_model as string
m_year as string
m_color as string
end type
constructor Car()
end constructor
constructor Car( _
byref theBrand as const string, _
byref theModel as const string, _
byref theYear as const string, _
byref theColor as const string )
m_brand = theBrand
m_model = theModel
m_year = theYear
m_color = theColor
end constructor
destructor Car()
end destructor
property Car.brand() as string
return( m_brand )
end property
property Car.model() as string
return( m_model )
end property
property Car.year() as string
return( m_year )
end property
property Car.color() as string
return( m_color )
end property
/'
This class defines a simple table of cars
'/
type CarTable
public:
declare constructor()
declare destructor()
declare property records() as uinteger
declare function record( _
byval as uinteger ) as ICarRecord ptr
declare sub addRecord( _
byval as ICarRecord ptr )
declare sub showReport()
private:
m_recordCount as uinteger
m_records( any ) as ICarRecord ptr
end type
constructor CarTable()
end constructor
destructor CarTable()
for i as integer = 0 to m_recordCount - 1
if( m_records( i ) <> 0 ) then
delete( m_records( i ) )
end if
next
end destructor
property CarTable.records() as uinteger
return( m_recordCount )
end property
function CarTable.record( _
byval anIndex as uinteger ) as ICarRecord ptr
return( m_records( anIndex ) )
end function
sub CarTable.addRecord( _
byval aRecord as ICarRecord ptr )
m_recordCount += 1
redim preserve m_records( 0 to m_recordCount - 1 )
m_records( m_recordCount - 1 ) = aRecord
end sub
sub CarTable.showReport()
color( 0, 7 )
? "Records in table: "; m_recordCount
? "Brand", "Model", , "Year", "Color", ""
color( 7, 0 )
for i as integer = 0 to m_recordCount - 1
? m_records( i )->brand;
? tab( 15 ); m_records( i )->model;
? tab( 43 ); m_records( i )->year;
? tab( 57 ); m_records( i )->color
next
end sub
sub drive( byval aCar as ICarRecord ptr )
? "Currently driving: "; aCar->brand; " "; aCar->model
end sub
/'
Main code
'/
'' Create a table
var aCarTable = CarTable()
'' Add some records
aCarTable.addRecord( new Car( _
"Ford", "Mustang", "1968", "Red" ) )
aCarTable.addRecord( new Car( _
"Alfa Romeo", "Tipo 158 Alfetta", "1947", "Blue" ) )
aCarTable.addRecord( new Car( _
"Acura", "NSX", "1992", "Green" ) )
'' And show the number of records present in the table
aCarTable.showReport()
?
'' Simple polymorphic function
for i as integer = 0 to aCarTable.records - 1
drive( aCarTable.record( i ) )
next
/'
The records in the table are all read-only, but you can easily
implement a property setter to update its values if you like.
However, sometimes it's easier to just remove the object from the
table, create a new one, and add it to the table again. This is
especially true if you keep the array sorted by, say, brand or
year.
'/
sleep()
This code implements a very simple table of cars. Only barebones functionality is implemented (namely, adding a record to the table and presenting a report). However, the table is polymorphic, and will add any record that conforms to the ICarRecord interface. I provided a simple, generic Car class that has some read-only properties, and a polymorphic drive() procedure that 'drives' each car present in the table. Is this what you're trying to do? Or are there
specific methods to call for each model of car?