c#string to variable

时间:2015-08-07 22:55:38

标签: c# string variables

我想将字符串用作变量( string + int = variable )

public DataTable Add_new(string t1,string t2,string t3,string t4,
                         string t5,string t6,string t7,string t8,
                         string t9,string t10)
    {
        OleDbParameter[] param = new OleDbParameter[10];

        for (int i = 1; i < 11; i++)
        {
            param[i-1] = new OleDbParameter(""+i+"", OleDbType.VarChar);
            param[i-1].Value = ("t"+i);
        }
        DataTable dt = new DataTable();
        dt = DAL.selectdata("Add_Code", param);
        return dt;
    }

4 个答案:

答案 0 :(得分:3)

您可以将值放在数组中,然后可以通过索引访问它们:

public DataTable Add_new(string t1,string t2,string t3,string t4,string t5,string t6,string t7,string t8,string t9,string t10) {
  string[] t = { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10 };

  OleDbParameter[] param = new OleDbParameter[10];

  for (int i = 1; i < 11; i++) {
    param[i-1] = new OleDbParameter(i.ToString(), OleDbType.VarChar);
    param[i-1].Value = (t[i - 1]);
  }
  DataTable dt = new DataTable();
  dt = DAL.selectdata("Add_Code", param);
  return dt;
}

另一种方法是将参数转换为params数组。你以相同的方式调用它,但它到达时是一个数组:

public DataTable Add_new(params string[] t) {
  if (t.Length != 10) throw new ArgumentsException("The method should be called with 10 parameters.");

  OleDbParameter[] param = new OleDbParameter[10];

  for (int i = 1; i < 11; i++) {
    param[i-1] = new OleDbParameter(i.ToString(), OleDbType.VarChar);
    param[i-1].Value = (t[i - 1]);
  }
  DataTable dt = new DataTable();
  dt = DAL.selectdata("Add_Code", param);
  return dt;
}

注意:您发送到方法中的那些值可能有一些含义。您应该考虑为参数设置有意义的名称,也许只需编写用于创建十个参数的代码。有一个循环这样做很方便,但维护起来不是很容易。

答案 1 :(得分:2)

为什么不把它传递给:

map

而不是

List<String> T;

然后:

string t1,string t2,string t3,string t4,string t5,string t6,string t7,string t8,string t9,string t10

所以它将是

for (int i = 1; i < 11; i++)
{
     param[i-1] = new OleDbParameter(""+i+"", OleDbType.VarChar);
     param[i-1].Value = (T[i-1]);
}

答案 2 :(得分:0)

您的实施没有意义。无论如何,以下内容适用于您的请求。

public DataTable Add_new(string t1, string t2, string t3, string t4,
                         string t5, string t6, string t7, string t8,
                         string t9, string t10)
        {
            OleDbParameter[] param = new OleDbParameter[10];
        for (int i = 1; i < 11; i++)
        {
            param[i - 1] = new OleDbParameter("" + i + "", OleDbType.VarChar);

            switch("t" + i)
            {
                case "t1":
                    param[i - 1].Value = t1;
                    break;
                case "t2":
                    param[i - 1].Value = t2;
                    break;
               //Recreate up to t10
            }
        }
        DataTable dt = new DataTable();
        dt = DAL.selectdata("Add_Code", param);
        return dt;
    }

答案 3 :(得分:-1)

试试这个

public DataTable Add_new(params string[] t)
{
    OleDbParameter[] param = new OleDbParameter[10];

    for (int i = 0; i <= 10; i++)
    {
        var paramName = String.Format("t{0}", i+1);
        param[i] = new OleDbParameter(paramName, OleDbType.VarChar);
        param[i].Value = (t[i]);
    }
    DataTable dt = new DataTable();
    dt = DAL.selectdata("Add_Code", param);
    return dt;
}