TStringList中AddObject使用

时间:2021-10-30 16:29:42

结构体定义

PYpType=^TYpType;
 TYpType=record
    yfcode:string;
    ypcode:string;
    YpUnitPrice:Currency;
    YpRetailPrice:Currency;
    YpCostPrice:Currency;
    YpTotal:Currency;
    CkTotal:Currency;
    YpUseDT:string;
    YpAddr:string;
    Dwcode:string;
    CzyCode:string;
    Update_DT:TDateTime;
    IsDelete:Integer;
    IsApprove:Integer;
 end;

加入结构体

function GetYfcodes: TStringList;

var
  sTemp, sTemp1: string;
  i: Integer;
  kcTotal, ckTotal: Currency;
  YpType: PYpType;
begin
  Result := TStringList.Create;
  if not dsDetail.IsEmpty then
  begin
    dsDetail.First;
    sTemp := '';
    sTemp1 := '';
    kcTotal := 0;
    ckTotal := 0;
    i := 0;
    while not dsDetail.Eof do
    begin
      sTemp1 := Format('%s,%s,%s,%s', [TCustomObj(cbbyf.Items.Objects[cbbyf.ItemIndex]).Code,
        dsDetail.FieldByName('YpCode').AsString,
          dsDetail.FieldByName('YpUnitPrice').AsString,
          dsDetail.FieldByName('YpRetailPrice').AsString]);
      if sTemp <> sTemp1 then
      begin
        sTemp := Format('%s,%s,%s,%s', [TCustomObj(cbbyf.Items.Objects[cbbyf.ItemIndex]).Code,
          dsDetail.FieldByName('YpCode').AsString,
            dsDetail.FieldByName('YpUnitPrice').AsString,
            dsDetail.FieldByName('YpRetailPrice').AsString]);
        if i > 0 then
        begin
          YpType^.YpTotal := kcTotal;
          YpType^.CkTotal := ckTotal;
          kcTotal := 0;
          ckTotal := 0;
          Result.AddObject(YpType^.ypcode, TObject(YpType));
          i := 0;
        end;
        New(YpType);
        YpType^.yfcode := TCustomObj(cbbyf.Items.Objects[cbbyf.ItemIndex]).Code;
        YpType^.ypcode := dsDetail.FieldByName('YpCode').AsString;
        YpType^.YpUnitPrice := dsDetail.FieldByName('YpUnitPrice').AsCurrency;
        YpType^.YpRetailPrice := dsDetail.FieldByName('YpRetailPrice').AsCurrency;
        YpType^.YpCostPrice := 0;
        YpType^.YpAddr := cbbypddr.Value;
        YpType^.YpUseDT := FormatDateTime('YYYY-MM-DD', dtyxq.Date);
        YpType^.Update_DT := Now;
        YpType^.Dwcode := TCustomObj(cbbdw.Items.Objects[cbbdw.ItemIndex]).Code;
        YpType^.IsDelete := 0;
        YpType^.IsApprove := 0;
        YpType^.CzyCode := TCustomObj(cbbczy.Items.Objects[cbbczy.ItemIndex]).Code;
      end;
      kcTotal := kcTotal + dsDetail.FieldByName('YpTotal').Value;
      ckTotal := ckTotal + dsDetail.FieldByName('CkTotal').Value;
      dsDetail.Next;
      if dsDetail.Eof then
      begin
        YpType^.YpTotal := kcTotal;
        YpType^.CkTotal := ckTotal;
        Result.AddObject(YpType^.ypcode, TObject(YpType));
      end;
      Inc(i);
    end;
  end;
end;

使用和释放结构体

procedure SaveMain;

var
  ypStr: TStringList;
  i: Integer;
  YpType: PYpType;
  FHeaderID: Integer;
begin
  if not CheckInput then Exit;
  ypStr := GetYfcodes;
  try
    try
      for I := 0 to ypStr.Count - 1 do
      begin
        YpType :=PYpType(ypstr.Objects[i]);
        if FDataSetMode = dsEdit then
          dsMaster.Edit
        else if
          FDataSetMode = dsAdd then
          dsMaster.Append
        else
          dsMaster.Append;
        FHeaderID := GetNextId('YP_BatchMergeHeader');
        dsMaster.FieldByName('HeaderID').Value := FHeaderID;
        dsMaster.FieldByName('YfCode').Value := YpType^.yfcode;;
        dsMaster.FieldByName('YpCode').Value := YpType^.ypcode;
        dsMaster.FieldByName('YpUnitPrice').Value := YpType^.YpUnitPrice;
        dsMaster.FieldByName('YpRetailPrice').Value := YpType^.YpRetailPrice;
        dsMaster.FieldByName('YpCostPrice').Value := YpType^.YpCostPrice;
        dsMaster.FieldByName('YpTotal').Value := YpType^.YpTotal-YpType^.CkTotal;
        dsMaster.FieldByName('CkTotal').Value := 0;
        dsMaster.FieldByName('YpAddr').Value := YpType^.YpAddr;
        dsMaster.FieldByName('YpUseDT').Value := YpType^.YpUseDT;
        dsMaster.FieldByName('Update_DT').Value := YpType^.Update_DT;
        dsMaster.FieldByName('DwCode').Value := YpType^.Dwcode;
        dsMaster.FieldByName('CzyCode').Value := YpType^.CzyCode;
        dsMaster.FieldByName('IsDelete').Value := YpType^.IsDelete;
        dsMaster.FieldByName('IsApprove').Value := YpType^.IsApprove;
        dsMaster.Post;
      end;
    except
      on E: Exception do
      begin
        MyMsgBox(MsgSaveFail + ':' + e.Message);
        SetLogTxt(e.Message);
        Exit;
      end;
    end;
  finally
    //释放PYpType
    for i := 0 to ypStr.Count - 1 do
    begin
      if Assigned(ypStr.Objects[i]) then
        Dispose(PYpType(ypStr.Objects[i]));
    end;
    ypStr.Free;
  end;
end;