In C #, how does one return a structure array in function parameters?

Source: Internet
Author: User

After studying it for a day, I tried to return a structure array in the parameters of the function.

This is for you to share and save your learning time.

Instance:
/// <Summary>
/// User Information Structure
/// </Summary>
Public struct TUSER
{
Public string LoginName;
Public string LoginPass;
Public string TrueName;
Public string EMail;
Public int Level;
}

/// <Summary>
/// Obtain all registered users
/// </Summary>
/// <Param name = "USERINFO"> returns the user information structure. </param>
Public bool GetUsers (out TUSER [] G_USERINFO)
{
USEROP. TUSER [] f_TUSER;
Int CurRec = 0; // initialization record counter
Int rec_count = 0; // calculate the total number of records
SqlConnection myConn = new SqlConnection (connectionString );
String cmd = "select * from" + m_Table; // SQL query statement
SqlCommand mycmd = new SqlCommand (cmd, myConn );
Mycmd. Connection. Open (); // Open the Connection
SqlDataReader adr = null; // create a data reader
Try
{
Adr = mycmd. ExecuteReader ();
While (adr. Read ())
{
Rec_count + = 1;
}
MyConn. Close (); // Close the database connection
Mycmd. Connection. Open (); // Open the database Connection
Try
{
Adr = mycmd. ExecuteReader ();
F_TUSER = new USEROP. TUSER [rec_count];
While (adr. Read ())
{
F_TUSER [CurRec]. LoginName = adr ["US_LOGINNAME"]. ToString ();
F_TUSER [CurRec]. LoginPass = adr ["US_LOGINPASS"]. ToString ();
F_TUSER [CurRec]. TrueName = adr ["US_TrueName"]. ToString ();
F_TUSER [CurRec]. EMail = adr ["US_EMail"]. ToString ();
F_TUSER [CurRec]. Level = Int32.Parse (adr ["US_Level"]. ToString ());
CurRec + = 1;
}
G_USERINFO = f_TUSER;
MyConn. Close (); // Close the connection
Return true;
}
Catch
{
MyConn. Close ();
F_TUSER = new USEROP. TUSER [1];
F_TUSER [0]. LoginName = "";
F_TUSER [0]. LoginPass = "";
F_TUSER [0]. TrueName = "";
F_TUSER [0]. EMail = "";
F_TUSER [0]. Level = 0;
G_USERINFO = f_TUSER;
Return false;
}
}
Catch
{
MyConn. Close ();
F_TUSER = new USEROP. TUSER [1];
F_TUSER [0]. LoginName = "";
F_TUSER [0]. LoginPass = "";
F_TUSER [0]. TrueName = "";
F_TUSER [0]. EMail = "";
F_TUSER [0]. Level = 0;
G_USERINFO = f_TUSER;
Return false;
}

/////////////// Call the following in web form:
Private void Button3_Click (object sender, System. EventArgs e)
{
KSBM. USEROP. TUSER [] f_TUSER; // = new KSBM. USEROP. TUSER (); // Why not "new" A structure array? Because the imported data is what we need, we only need to open up a bucket.
KSBM. USEROP f_USOP = new KSBM. USEROP ();
Bool ret = f_USOP.GetUsers (out f_TUSER); // call the function to obtain the structure array and information
If (f_TUSER.Length! = 0) // if the data length is 0, no data is returned.
{
For (int iloop = 0; iloop <f_TUSER.Length; iloop ++) // traverses the array and outputs it to the browser of the client.
{
Response. Write (f_TUSER [iloop]. LoginName );
Response. Write (f_TUSER [iloop]. LoginPass );
Response. Write (f_TUSER [iloop]. TrueName );
Response. Write (f_TUSER [iloop]. EMail );
Response. Write (f_TUSER [iloop]. Level );
}
}
}

Related Article

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.