How do I return an array of structures in a function argument in C #?

Source: Internet
Author: User
function | Array Giddy brain went up for one day to implement an array of structures in the parameters of a function.

Now for everyone to share, save time for learning.

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>
Get all registered Users
</summary>
<param name= "USERINFO" > Return user Information Structure </param>
public bool Getusers (out tuser[] g_userinfo)
{
Userop. Tuser[] F_tuser;
int currec = 0; Initializing record counters
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 connection
SqlDataReader ADR = NULL; Creating a data reader
Try
{
ADR = myCMD. ExecuteReader ();
while (ADR. Read ())
{
Rec_count + 1;
}
Myconn.close (); To close a database connection
myCMD. Connection.Open (); Open a 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 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;
}


The following is called in the Web form:
private void Button3_Click (object sender, System.EventArgs e)
{
KSBM. Userop. Tuser[] F_tuser; = new KSBM. Userop. TUSER (); Why not "new" an array of structures? Because it's the data we want to get in, so just open up a storage space.
KSBM. Userop f_usop = new KSBM. Userop ();
BOOL Ret=f_usop. Getusers (out F_tuser); Call function, get structure array and information
if (F_tuser. Length!=0)//data is 0 o'clock, indicating no return data.
{
for (int iloop=0;iloop<f_tuser. length;iloop++)///to traverse the array and output to the client's browser
{
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);
}
}
}


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.