Self-built utility (1)-result object

Source: Internet
Author: User

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;

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.