When designing our own functions, in addition to using the ref or out parameters, it is difficult to return multiple results,
We often want to know whether the operation is successful and provide a prompt, or return a value such as dataset and arraylist.
Therefore, a result object is created:
It contains three child objects,
A bool type is used to record whether the operation is successful;
A string used to record error information or prompt information;
An object is used to record the returned object. It can be a string, dataset, or another result ..
The general usage is as follows:
Assign a value to result:
Public result getinittable (){
Result rs = new result (false, String. Empty );
Dataset DS = new dataset ();
Datatable dt = new datatable ();
Try
{
Rs = new result (true, String. Empty, DT );
}
Catch (exception ex)
{
Console. Write (ex. Message );
}
Return Rs;
}
Use result:
Result rs = ts. getinittable ();
Datatable dt_dtlresult = (datatable) Rs. OBJ; // get the datatable in result
Result code:
Public class result
{
Private bool B _result;
Private string s_message;
Private object o_obj;
Public result (bool B _result, string s_message)
{
//
// Todo: add the constructor logic here
//
This. B _result = B _result;
This. s_message = s_message;
}
Public result (bool B _result, string s_message, object o_object)
{
//
// Todo: add the constructor logic here
//
This. B _result = B _result;
This. s_message = s_message;
This. o_obj = o_object;
}
Public bool success
{
Get
{
Return this. B _result;
}
Set
{
This. B _result = value;
}
}
Public String message
{
Get
{
Return this. s_message;
}
Set
{
This. s_message = value;
}
}
Public object OBJ
{
Get
{
Return this. o_obj;
}
Set
{
This. o_obj = value;
}
}