Container的使用

在Axapta中有一个基础类型Container,在某些场合下确实比较好用,axapta现有系统中用的也比较多,比如Runbase的pack()和unpack()方法就是用Container结合宏来实现的.
container可以看作是无类型的动态增长的数组,功能基本上等效于C#中的ArrayList.只不过container不能存放对象(Table对象还是可以存的),另外container可以作为数据库字段类型,在数据库中转化成img类型,可以看出container把数据序列化了.
另外由于Container存放的元素可以是Container,所以可以实现类似于二维数组的功能.今天遇到一个需求,做一张客户(供应商)对帐单,由于国内的财务需要按月汇总,而用户输入的查询时间可能是跨月的,并且不一定是从一号开始到月底结束的,比如用户输入如下时间段做查询 
2006-03-13~~~2006-05-27
这样按照国内的财务需求,需要分解成如下的时间段:
1.2006-03-13~~~2006-03-31
2.2006-04-01~~~2006-04-40
3.2006-05-01~~~2006-05-27
分别统计上述三段时间的交易信息并分别汇总,这样就要求把任意一段时间,按月分解,当然这个有很多种做法,不过感觉用container实现这个小功能还是挺方便的,代码如下:
None.gif static  Container GetDatePeriod(TransDate fromDate,TransDate toDate)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Container cPeriod,cPeriodForOneMonth;
InBlock.gif    
int i;
InBlock.gif    
int day;
InBlock.gif    TransDate transDate;
InBlock.gif    
int gYearPart,gMonthPart;
InBlock.gif
InBlock.gif    
//得到某一个月的天数
InBlock.gif
    int GetDay(int yearPart,int monthPart)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
switch(monthPart)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
case 1:
InBlock.gif            
case 3:
InBlock.gif            
case 5:
InBlock.gif            
case 7:
InBlock.gif            
case 8:
InBlock.gif            
case 10:
InBlock.gif            
case 12:
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                day 
= 31;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
case 4:
InBlock.gif            
case 6:
InBlock.gif            
case 9:
InBlock.gif            
case 11:
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                day 
= 30;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
case 2:
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif               
if(!(yearPart mod 4))
InBlock.gif                    day 
= 29;
InBlock.gif                
else
InBlock.gif                    day 
= 28;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
default:
InBlock.gif                
throw error("Error input month!");
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif        
//得到一个完整月的开始和结束日期
InBlock.gif
    Container GetPeriodForOneMonth(int yearPart,int monthPart)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ;
InBlock.gif        GetDay(yearPart,monthPart);
InBlock.gif        
return [str2Date(int2str(yearPart)+"-"+int2Str(monthPart)+"-01",321),
InBlock.gif                str2Date(int2str(yearPart)
+"-"+int2Str(monthPart)+"-"+int2Str(day),321)];
ExpandedSubBlockEnd.gif    }
 ;
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
//插入开始和结束日期中间完整的月
InBlock.gif
    for(i = mthofyr(fromDate)+1;i<mthofyr(toDate);i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cPeriod 
= conins(cPeriod,conlen(cPeriod)+1,GetPeriodForOneMonth(year(toDate),i));
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//插入开始月
InBlock.gif
    GetDay(year(fromDate),mthofYr(fromDate));
InBlock.gif    gYearPart 
= year(fromDate);
InBlock.gif    gMonthPart 
= mthofYr(fromDate);
InBlock.gif
InBlock.gif    transDate 
= str2Date(int2Str(gYearPart)+"-"+int2Str(gMonthPart)+"-"+int2Str(day),321);
InBlock.gif    cPeriod 
= conins(cPeriod,1,[fromDate,transDate]);
InBlock.gif
InBlock.gif    
//插入结束月
InBlock.gif
    if(mthofYr(fromDate)!=mthofYr(toDate))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        gYearPart 
= year(toDate);
InBlock.gif        gMonthPart 
= mthofYr(toDate);
InBlock.gif
InBlock.gif        transDate 
= str2Date(int2Str(gYearPart)+"-"+int2Str(gMonthPart)+"-01",321);
InBlock.gif        cPeriod 
= conins(cPeriod,conlen(cPeriod)+1,[transDate,toDate]);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return cPeriod;
InBlock.gif
ExpandedBlockEnd.gif}

调用的代码如下:
None.gif static   void  Main(Args arg)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Container c,cPeriod;
InBlock.gif    TransDate fromDate;
InBlock.gif    TransDate toDate;
InBlock.gif    
int i;
InBlock.gif    ;
InBlock.gif    fromDate 
=  str2Date("2006-02-06",321);
InBlock.gif    toDate 
= str2Date("2006-09-30",321);
InBlock.gif    c 
= Class2::GetDatePeriod(fromDate,toDate);
InBlock.gif    
for(i = 1 ;i<=conlen(c);i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cPeriod 
= conpeek(c,i);
InBlock.gif        fromDate 
= conpeek(cPeriod,1);
InBlock.gif        toDate 
= conpeek(cPeriod,2);
InBlock.gif        print date2StrXpp(fromDate)
+"---"+Date2StrXpp(toDate);
ExpandedSubBlockEnd.gif    }

InBlock.gif    pause;
InBlock.gif
ExpandedBlockEnd.gif}
当然由于对帐表要统计年结余额,所以用户输入的查询日期一般不允许跨年度,所以这个程序也就没有处理跨年度的情况了.

转载于:https://www.cnblogs.com/Farseer1215/archive/2006/10/24/538489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值