不能从班级中返回变量

时间:2015-02-24 06:18:32

标签: c#

 class cAccess
    {
        private static string usn1;

        public static string usnt
        {
            set
            {
                usn1 = value;
            }
            get
            {
                string[] s = new string[3];
                DataSet dsa = new DataSet();
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand("select * from access where id=" + usn1, connection.setCon());
                adp.Fill(dsa, "t");
                DataRow DBrow = dsa.Tables["t"].Rows[0];
                for (int i = 0; i < 3; i++)
                {
                    s[i] = DBrow.ItemArray.GetValue(i).ToString();
                }
                return s;
            }
        }
    }

我想从类返回s数组,但是当我开始调试时,它会显示以下错误:

  

当前上下文中不存在名称''。代码工作正常   但是不能回来。

1 个答案:

答案 0 :(得分:1)

您无法将string[]返回string

尝试:

class cAccess
{
    private static string usn1;

    public string usnt
    {
      set
      {
        usn1 = value;
      }
      get
      {
        StringBuilder s = new StringBuilder();
        DataSet dsa = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter();
        adp.SelectCommand = new SqlCommand("select * from access where id=" + usn1, connection.setCon());
        adp.Fill(dsa, "t");
        DataRow DBrow = dsa.Tables["t"].Rows[0];
       for (int i = 0; i < 3; i++)
       {
         s.Append(DBrow.ItemArray.GetValue(i).ToString());
       }

       usn1 = s.ToString();
       return(usn1);
 }