如何通过名称找到并在对象集合中获取Item?
procedure TfoMain.InitForm;
begin
// Liste des produits de la pharmacie 1
FListeDispoProduit := TListeDispoProduit.Create(TProduit);
with (FListeDispoProduit) do
begin
with TProduit(Add) do
begin
Name := 'Produit 01';
CIP := 'A001';
StockQty := 3;
AutoRestock := 1;
QtyMin:= 2;
end;
with TProduit(Add) do
begin
Name := 'Produit 02';
CIP := 'A002';
StockQty := 5;
AutoRestock := 0;
QtyMin:= 2;
end;
function getProductByName(productName: String): TProduit;
var
i : integer;
begin
for i := 0 to fProductList.Count -1 do
begin
if (TProduit(fProductList.Items[i]).Name = productName)
Result :=
end;
end;
我想编辑有关产品名称的数量。
我该怎么做? 谢谢
答案 0 :(得分:2)
如果您的集合对象是TCollection
,那么它具有Items
属性(您应该在文档或源代码中看到它)。使用它及其Count
属性编写一个循环,检查每个项目是否与目标匹配。
var
i: Integer;
begin
for i := 0 to Pred(FListeDespoProduit.Count) do begin
if TProduit(FListeDespoProduit.Items[i]).Name = productName then begin
Result := TProduit(FListeDespoProduit.Items[i]);
exit;
end;
end;
raise EItemNotFound.Create;
end;
Items
是default property,这意味着您可以从代码中省略它,只需单独使用数组索引即可。您可以将其缩短为FListeDespoProduit.Items[i]
,而不是FListeDespoProduit[i]
。
答案 1 :(得分:0)
您的TProduit
工具(Add
)。它还没有实现(Get
)(或类似的东西)?
您是继承此代码吗?还有更多细节吗?
编辑:否则你必须自己创建Get
程序,可能是通过循环遍历列表并查找匹配,然后返回它。
答案 2 :(得分:0)
function getProductByName(productName: String): TProduit;
var
i : integer;
begin
for i := 0 to fProductList.Count -1 do
begin
if (TProduit(fProductList.Items[i]).Name = productName)
Result := TProduit(fProductList.Items[i]); // this???
end;
end;
然后你可以去:
MyProduit := getProductByName('banana');
MyProduit.StockQty := 3;
或者你想要的任何东西。