0

I want use string as variable ( 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;
    }
2
  • how convert ("t"+i) to variable t1 not "t1" Commented Aug 7, 2015 at 22:57
  • You can click "edit" to update your question. Commented Aug 7, 2015 at 23:32

4 Answers 4

3

You can put the values in an array, then you can access them by index:

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;
}

Another approach is to turn the parameters into a params array. You call it the same way, but it is an array when it arrives:

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;
}

Note: Those values that you send into the method probably has some meaning. You should consider to have meaningful names for the parameters, and perhaps just write the code for creating ten parameters. Having a loop doing it is convenient, but it's not very easy to maintain.

Sign up to request clarification or add additional context in comments.

Comments

2

Why not pass it as a:

List<String> T;

instead of

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

and then:

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

so it will be

public DataTable Add_new(List<String> T) {

  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 Comment

The index in a list (just as an array) is zero-based, so that should be T[i - 1].
0

Your implementation doesn't make sense. Regardless, the following would work for your request.

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;
    }

Comments

-1

Try this

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.