0

how can I find by name and get the Item in a collection of object ?

    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;

I want to edit qty about a product name.

How can I do this? thank you

TimeIsNear
  • 711
  • 4
  • 19
  • 38
  • You edited your question while I was writing my answer, and now it appears that you already know how to recognize an item in the list. Since you already know the answer, what are you really asking for? – Rob Kennedy Aug 12 '10 at 14:58

3 Answers3

2

If your collection object is a TCollection, then it has an Items property (which you should have been about to see in the documentation, or in the source code). Use that and its Count property to write a loop where you inspect each item to see whether it matches your target.

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 is a default property, which means you can omit it from your code and just use the array index by itself. Instead of FListeDespoProduit.Items[i], you can shorten it to just FListeDespoProduit[i].

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
0

Your TProduit implements (Add). It doesn't already implement (Get) (or something similar)?

Are you inheriting this code? Is there more detail?

Edit: otherwise you'll have to create the Get procedure yourself, possibly by looping over the list and finding a match, then returning it.

Bart
  • 19,692
  • 7
  • 68
  • 77
Tobiasopdenbrouw
  • 13,811
  • 1
  • 22
  • 27
  • 1
    In the same way you address it. Assign it to the Result variable and then "exit" or "break". The difference is that "exit" exits procedure while "break" only exits the "for i := 0 to fProductList.Count-1" cycle. – himself Aug 12 '10 at 15:09
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;

You can then go:

MyProduit := getProductByName('banana');
MyProduit.StockQty := 3;

Or whatever you wish.