自己构造公用程序(1)--Result对象

我们在设计自己的函数的时候,除了借助ref或者out参数,很难返回多个结果,
 我们往往希望知道操作是否成功,并且给出提示信息,或者还希望返回一个诸如DataSet,ArrayList的值.
 因此做了一个Result对象:
 里面包含三个子对象,
  一个bool型,用来记录操作是否成功;
  一个string,用来记录出错信息或者提示信息;
  一个object,用来记录返回的对象,可以是一个string,DataSet,或者也可以是另外一个Result等等..

大致运用如下:
 为Result赋值:

ExpandedBlockStart.gif ContractedBlock.gif public  Result getInitTable() dot.gif {
InBlock.gif            Result rs 
= new Result(false,string.Empty);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            DataTable dt 
= new DataTable();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dot.gif
InBlock.gif                rs 
= new Result(true,string.Empty,dt);
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.Write(ex.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return rs;
ExpandedBlockEnd.gif        }


使用Result:

None.gif Result rs = ts.getInitTable();
None.gif            DataTable dt_Dtlresult
= (DataTable)rs.OBJ;  // 得到Result中的DataTable

Result的代码:

None.gif public   class  Result
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private bool b_result;
InBlock.gif        
private string s_message;
InBlock.gif        
private object o_obj;
InBlock.gif
InBlock.gif        
public Result(bool b_result,string s_message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
InBlock.gif
            this.b_result=b_result;
InBlock.gif            
this.s_message=s_message;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif        
public Result(bool b_result,string s_message,object o_object)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
InBlock.gif
            this.b_result=b_result;
InBlock.gif            
this.s_message=s_message;
InBlock.gif            
this.o_obj=o_object;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool Success
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.b_result;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.b_result = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Message
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.s_message;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.s_message = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public object OBJ
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.o_obj;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.o_obj = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif




转载于:https://www.cnblogs.com/applegreen/archive/2005/04/04/131955.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值