Begi n

custKey := TCustomerKey.Create; custKey.CustNo := dataset [ 'CustNo']; custKey.Company := dataset ['Company']; self.Add(custKey, TCustomer.Create (custKey)); dataset.Next; end; end;

The demo program has a main form and a data module hosting a Client-DataSet85. The main form has a ListView control that is filled when a user presses the only button.

After loading the data in the dictionary, the btnPopulateClick method uses an enumerator on the dictionary's keys:

85 You might want to replace the ClientDataSet component with a real dataset, expanding the example considerably in terms of usefulness, as you could run a query for the keys and a separate one for the actual data of each single TCustomer object. I have similar code, but adding it here would have distracted us too much from the goal of the example, which is experimenting with a generic dictionary class.

procedure TFormCustomerDictionary.btnPopu1ateClick(

Sender: TObject); var custkey: TCustomerKey; listItem: TListItem; begi n

DataModulel.ClientDataSetl.Active := True; CustDict.LoadFromDataSet(DataModulel.ClientDataSetl);

for custkey in CustDict.Keys do begi n listItem := ListViewl.Items.Add; listItem.Caption := custkey.Company; 1istItem.SubItems.Add(FloatTOStr (custkey.CustNo)); listItem.Data := custkey;

end;

end;

This fills the first two columns of the ListView control, with the data available in the key objects. Whenever a user selects an item of the ListView control, though, the program will fill a third column:

procedure TFormCustomerDictionary.ListViewlSe1ectItem( Sender: TObject; Item: TListItem; Selected: Boolean); var aCustomer: TCustomer; begi n aCustomer := CustDict.Items [Item.data]; Item.SubItems.Add(IfThen (aCustomer.State <> '', aCustomer.State + ' + aCustomer.Country, aCustomer.Country));

end;

The method above gets the object mapped to the given key, and uses its data. Behind the scenes, the first time a specific object is used, the property access method triggers the loading of the entire data for the TCustomer object.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment