日期:2014-05-18  浏览次数:20702 次

请教一下关于params的问题
public DataSet SP_Common(int iKey, params string[] paramsArr){。。。}

调用时SP_Common(iKey, this.ctrlCity.Value);说has some invalid arguments,我把声明params参数改成params object[]paramsArr也不行.难道调用时非要写成new string[]{this.ctrlCity.Value}?

------解决方案--------------------
MSDN:
public class MyClass 
{

public static void UseParams(params int[] list) 
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}

public static void UseParams2(params object[] list) 
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}

static void Main() 
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test"); 

// An array of objects can also be passed, as long as
// the array type matches the method being called.
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}
------解决方案--------------------
是这样的。
------解决方案--------------------
params 是指这个参数是个数组

SP_Common(iKey, this.ctrlCity.Value);

此时的this.ctrlCity.Value不是数组
类型不匹配,所以错误

new string[]{this.ctrlCity.Value}
时,传的是数组,故正确

当然也可以
public DataSet SP_Common(int iKey, string[] paramsArr){。。。} 
去掉params 关键字
------解决方案--------------------
其实是这样的

函数:

public void Test1(String p1, params Int32[] p2);

调用者可以传入N个参数(N>2)

Test("Hello world",6,4,3,7,7,7,32,3198,651,1086,13,11,458,1865);
--------------------------------------------
这些对应参数p2

这些参数将会填充局部变量p2
------解决方案--------------------
public DataSet SP_Common(int iKey, params string[] paramsArr){。。。} 

调用时SP_Common(iKey, this.ctrlCity.Value);
应该是没有问题的.
会不会是其它问题引起的错误呢.
你把代码都贴出来看看吧