Prevent unnecessary DBFRecord

Aug 23, 2015 at 7:44 PM
Reason:
Every time a cell in WPF DataGrid needs access to the underlaying collection it tries to get an item through List:Current(). Therefore DBServer4DB creates every time a new DBFRecord. These DBFRecords should mbe saved for future use.

Solution:
  • We define a new class to collect all DBFReords for that DBF
CLASS DBFRecords INHERIT System.Collections.Generic.Dictionary<INT,DBFRecord>
END CLASS
  • In the constructor if DBServer4DB we init this collection

    CONSTRUCTOR( oFile, lShareMode, lReadOnlyMode, xDriver, aRdd )
    SUPER( oFile, lShareMode, lReadOnlyMode, xDriver, aRdd )
    SELF:DBFRecords := DBFRecords{}
    
    RETURN
  • in the current method we first look, if a DBFRecord for this RecNo exist. If not, we creatre and add to the collection, then return this record.

    /// This instantiates an object which represents all column values of the current record.
    METHOD Current() AS DBFRecord
    LOCAL NewRecord AS DBFRecord
    IF !SELF:DBFRecords:ContainsKey( (INT)SELF:RecNo )
            NewRecord := DBFRecord{SELF:RecNo,SELF}
            DBFRecords:Add( SELF:RecNo, NewRecord )
    ELSE
        NewRecord := DBFRecords[SELF:RecNo]
    ENDIF
    RETURN NewRecord
  • For new records we modify the Add() method

    /// All methods and properites in this region are defined in the IList interface, so they have to be implemented.
    /// As we do not need all of them in the demonstration, we leave them unimplemented.
    VIRTUAL METHOD Add( value AS OBJECT ) AS INT
    /// We get a newly created DBFRecord object here which has no reference to the server
    LOCAL record := (DBFRecord) value AS DBFRecord
    record:ParentServer := SELF
    SELF:Append()
    record:RecordNumber := SELF:RecCount
    SELF:DBFRecords:Add( record:RecordNumber, record )
    RETURN 0
  • we can now define Contains(), but I really don't its use

    VIRTUAL METHOD Contains( value AS OBJECT ) AS LOGIC
    RETURN SELF:DBFRecords:ContainsKey( ((DBFRecord)value):RecordNumber )
    
Frank