TStringList中AddObject使用-阿里云开发者社区

开发者社区> 云计算> 正文

TStringList中AddObject使用

简介: 结构体定义 PYpType=^TYpType; TYpType=record    yfcode:string;    ypcode:string;    YpUnitPrice:Currency;    YpRetailPrice:Currency;    YpCostPrice:Currenc...

结构体定义

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;

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章