在c#函数中返回一个值

时间:2011-01-06 10:36:18

标签: c# asp.net return-value

我在.CS文件中有一个功能。我想将函数中提取的许多值返回到.aspx.cs文件(同一项目中的两个文件)

如何从以下函数返回值(.CS文件代码如下):

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Collections;
using System.IO;


using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;

/// <summary>
/// Summary description for Data
/// </summary>
public class Data
{
    public Data()
    {
        //
        // TODO: Add constructor logic here
        //



    }
    public void dat(AjaxControlToolkit.HTMLEditor.Editor Editor1,TextBox TextBox3, TextBox TextBox4, TextBox TextBox2, TextBox TextBox1, DropDownList DropDownList1, DropDownList DropDownList3, HiddenField HiddenField1, HiddenField HiddenField4)
{    
    String dbDate = DateTime.ParseExact(TextBox3.Text, "dd/mm/yyyy", null).ToString("yyyy-mm-dd");

        String respo1 = "";
        String respo2 = "";
        String editorcont1 = "";
        int res = 0;
        String sb = "";

        String petitioner = "";
        String petitioner1 = "";
        String resp = "";
        String resp1 = "";

        String respondants = "";
        String addr1 = "";
        String addr2 = "";


        var order = "";

        String nextdate = "";
        String nextdate1 = "**/**/****";
        String judge1 = "";
        String judge2 = "";
        String judge3 = "";
        String advocates = "";


        String a = DropDownList1.SelectedItem.Value;
        String b = DropDownList3.SelectedItem.Value.PadLeft(3, '0');
        String c = TextBox1.Text.PadLeft(5, '0').ToString();
        String d = TextBox2.Text.ToString();
        String digit = a + b + c + d;
        String jjj = "";


        try
        {
            OdbcConnection casetype = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=10.155.160.130;Database=testcase;User=root;Password=;Option=3;");
            casetype.Open();

            String petresquery1 = "select fil_no from testcase.main where reg_no =? OR fil_no=?";
            OdbcCommand petrescmd1 = new OdbcCommand(petresquery1, casetype);
            petrescmd1.Parameters.AddWithValue("?", digit);
            petrescmd1.Parameters.AddWithValue("?", digit);
            OdbcDataReader respetMyReader1 = petrescmd1.ExecuteReader();

            while (respetMyReader1.Read())
            {
                String fil_no = respetMyReader1["fil_no"].ToString();
                HiddenField4.Value = fil_no;
                //Response.Write(HiddenField4.Value);
            }


            string showmodifquery = "select notice from notice_aspx where fil_no=?";

            OdbcCommand showmodifcmd = new OdbcCommand(showmodifquery, casetype);

            showmodifcmd.Parameters.AddWithValue("?", HiddenField4.Value);
            OdbcDataReader showmodifMyReader = showmodifcmd.ExecuteReader();

            if (showmodifMyReader.Read())
            {
                String showmodif1 = showmodifMyReader["notice"].ToString();

               Editor1.Content = showmodif1;
            }
            else
            {
                //************to get case type     
                string casetypequery = "select casename from casetype where skey=?";
                //************to get pet  res
                string petresquery = "select pet_name,res_name from testcase.main where reg_no =? OR fil_no=?";
                //*******to get respondants 
                string respoquery = "SELECT sr_no,partyname,addr1,addr2 FROM testcase.party where fil_no=? and pet_res='R'order by sr_no,party_lh,party_lh2,party_lh3,party_lh4,party_lh5";
                //*******to get order
                string ordequery = "select orde from testcase.orddetpabak where fil_no=? and orderdate=?";
                //*********to get next date
                string nextdatequery = "SELECT next_dt FROM testcase.heardt where fil_no=? and next_dt>?;";
                //*********to get jud1
                string jud1query = "select jname from testcase.orddetpabak,testcase.judge where orddetpabak.jud1 = judge.jcode and fil_no=? and orderdate=?;";
                //*********to get jud2
                string jud2query = "select jname from testcase.orddetpabak,testcase.judge where orddetpabak.jud2 = judge.jcode and fil_no=? and orderdate=?;";
                //*********to get jud3
                string jud3query = "select jname from testcase.orddetpabak,testcase.judge where orddetpabak.jud3 = judge.jcode and fil_no=? and orderdate=?;";
                //*********to get advocates
                string advquery = "SELECT adv FROM testcase.orddetpabak where fil_no=? and orderdate=?";



                //************to get case type 
                OdbcCommand casetypecmd = new OdbcCommand(casetypequery, casetype);
                String casetypefromdropdown = DropDownList3.SelectedItem.ToString();
                casetypecmd.Parameters.AddWithValue("?", casetypefromdropdown);
                using (OdbcDataReader casetypeMyReader = casetypecmd.ExecuteReader())
                {
                    while (casetypeMyReader.Read())
                    {
                        String casename = casetypeMyReader["casename"].ToString();
                        HiddenField1.Value = casename;
                    }
                }

                //************to get pet  res
                OdbcCommand petrescmd = new OdbcCommand(petresquery, casetype);
                petrescmd.Parameters.AddWithValue("?", digit);
                petrescmd.Parameters.AddWithValue("?", digit);
                using (OdbcDataReader respetMyReader = petrescmd.ExecuteReader())
                {
                    while (respetMyReader.Read())
                    {
                        petitioner1 = respetMyReader["pet_name"].ToString();

                        //petitioner1 = petitioner.Substring(0, 1) + petitioner.Substring(1).ToLower();
                        resp1 = respetMyReader["res_name"].ToString();
                        //resp1 = resp.Substring(0, 1) + resp.Substring(1).ToLower();

                        //String fil_no = respetMyReader["fil_no"].ToString();
                        //HiddenField4.Value = fil_no;
                        ////Response.Write(HiddenField4.Value);
                      // return petitioner1;
                    }
                    if (petitioner1 == "")
                    {
                        HttpContext.Current.Response.Write("<b><font color='red'>Wrong Entry!!!");
                    }
                }


                //*******to get respondants

                String respo = HiddenField4.Value;
                OdbcCommand respocmd = new OdbcCommand(respoquery, casetype);
                respocmd.Parameters.AddWithValue("?", respo);
                using (OdbcDataReader respoMyReader = respocmd.ExecuteReader())
                {
                    while (respoMyReader.Read())
                    {
                        respo1 = respoMyReader["sr_no"].ToString();
                        respo2 = respoMyReader["partyname"].ToString();
                        addr1 = respoMyReader["addr1"].ToString();
                        addr2 = respoMyReader["addr2"].ToString();
                        res = Convert.ToInt32(respo1);
                        //Response.Write(res);
                        //Response.Write(respo2);

                        //editorcont1 = "<table><tr><td width='10'>" + res + "</td><td>"+"<P align= 'left'>"+  respo2 +"</P></td>"+"</br>";

                        ////HiddenField7.Value = editorcont1;
                        //asd(editorcont1);

                        //respondants = respo2.Substring(0, 1) + respo2.Substring(1).ToLower();
                        //addr1 = addr1.Substring(0, 1) + addr1.Substring(1).ToLower();
                        //addr2 = addr2.Substring(0, 1) + addr2.Substring(1).ToLower();
                        editorcont1 = res + ")&nbsp;&nbsp;" + respo2 + "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + addr1 + "&nbsp;&nbsp;" + addr2 + "<br/>";
                        sb = sb + editorcont1;
                    }
                }

                //*******to get order
                OdbcCommand ordecmd = new OdbcCommand(ordequery, casetype);
                ordecmd.Parameters.AddWithValue("?", HiddenField4.Value);
                ordecmd.Parameters.AddWithValue("?", dbDate);
                using (OdbcDataReader ordeMyReader = ordecmd.ExecuteReader())
                {
                    while (ordeMyReader.Read())
                    {
                        order = ordeMyReader["orde"].ToString();
                    }

                }
                //*******to get nextdate
                OdbcCommand nextdatecmd = new OdbcCommand(nextdatequery, casetype);
                nextdatecmd.Parameters.AddWithValue("?", HiddenField4.Value);
                nextdatecmd.Parameters.AddWithValue("?", dbDate);
                using (OdbcDataReader nextdateMyReader = nextdatecmd.ExecuteReader())
                {
                    while (nextdateMyReader.Read())
                    {
                        nextdate = nextdateMyReader["next_dt"].ToString();

                    }
                    nextdate1 = Convert.ToDateTime(nextdate).ToString("dd/MM/yyyy");


                }

                //*********to get jud1
                OdbcCommand jud1cmd = new OdbcCommand(jud1query, casetype);
                jud1cmd.Parameters.AddWithValue("?", HiddenField4.Value);
                jud1cmd.Parameters.AddWithValue("?", dbDate);
                using (OdbcDataReader jud1MyReader = jud1cmd.ExecuteReader())
                {
                    while (jud1MyReader.Read())
                    {
                        judge1 = jud1MyReader["jname"].ToString();
                        jjj = "J";
                    }
                }


                //*********to get jud2
                OdbcCommand jud2cmd = new OdbcCommand(jud2query, casetype);
                jud2cmd.Parameters.AddWithValue("?", HiddenField4.Value);
                jud2cmd.Parameters.AddWithValue("?", dbDate);
                using (OdbcDataReader jud2MyReader = jud2cmd.ExecuteReader())
                {
                    while (jud2MyReader.Read())
                    {
                        judge2 = jud2MyReader["jname"].ToString();
                        jjj = "JJ";
                    }
                }


                //*********to get jud3
                OdbcCommand jud3cmd = new OdbcCommand(jud3query, casetype);
                jud3cmd.Parameters.AddWithValue("?", HiddenField4.Value);
                jud3cmd.Parameters.AddWithValue("?", dbDate);
                using (OdbcDataReader jud3MyReader = jud3cmd.ExecuteReader())
                {
                    while (jud3MyReader.Read())
                    {
                        judge3 = jud3MyReader["jname"].ToString();
                        jjj = "JJJ";
                    }
                }
                //*******to get advocates
                OdbcCommand advcmd = new OdbcCommand(advquery, casetype);
                advcmd.Parameters.AddWithValue("?", HiddenField4.Value);
                advcmd.Parameters.AddWithValue("?", dbDate);
                using (OdbcDataReader advMyReader = advcmd.ExecuteReader())
                {
                    while (advMyReader.Read())
                    {
                        advocates = advMyReader["adv"].ToString();
                    }

                }
                DateTime month = DateTime.Now;
                String tyear = TextBox2.Text;
                tyear = tyear.Remove(0, 2);

                String year = DateTime.Now.Year.ToString();
                year = year.Remove(0, 2);

                TextBox4.Text = order;
            }
        }
                    catch (Exception er)
        { }

}
}

我想通过倡导者,jjj,tyear,year,petitioner1,resp1,sb等......我怎么能这样做?

3 个答案:

答案 0 :(得分:7)

如果要从方法返回多个值,则需要创建一个类来执行此操作。

例如:

public class Product
{
   int Id { get; set;} 
   string Name { get; set;} 
   double Value { get; set;}
}

然后当您想从方法中返回产品时:

public Product GetProduct(string Id) 
{
   Product product; 
   // load product here
   return product; 
} 

答案 1 :(得分:6)

将所有相关值封装在一个新类型中,并使您的方法返回该类型的实例。

哦,并尽量避免使用这些巨大的方法 - 它们是可读性,测试等的噩梦。

答案 2 :(得分:1)

函数只返回一个值。对多个返回值使用“out”或“ref”参数。

见这里:http://www.yoda.arachsys.com/csharp/parameters.html