使用webservice后,'params'可选参数在.net应用程序中不起作用 [英] 'params ' optional parameter is not working in .net application after consuming webservice
问题描述
我在.net 4.0中创建了xml webservice(visual studio 2010)
[WebMethod]
public XmlDocument Showmethod(int rollno, params string [] DSFromaddress)
{
- 编码部分 -
}
使用addwebreference在.net中使用web方法后,我可以访问此方法。
for ex:webservice name webshow
webshow.webclass obj = new webshow.webclass();
XmlNode resutls;
results = obj.Showmethod(5);
i没有传递第二个参数,因为它是可选参数in .net webservice中的Showmethod。
但是它显示错误方法'Showmethod'没有重载需要1个参数。
为什么params可选参数不工作。如何解决这个问题?
需要帮助。
谢谢,
这只不过是用于隐式创建的语法糖通话中的数组:
ShowMethod( 0 , 1, 2);
严格等同于
ShowMethod( 0 , new string [] { 1, 2 ,});
然而,你可以写
// 适用于我:
ShowMethod( 0 );
与<相同br />
ShowMethod( 0 , new string [ 0 ]);
// 注意:非空数组,但是空数组
您的问题可以用简单的方法解释:您显示的代码与您使用的代码不完全相同;你混淆了什么东西,就像那样。如果您不同意,请写一个简单的自包含代码示例来说明问题。首先,它将帮助您自己理清事物。如果没有,请在您的问题中显示此代码。
I created xml webservice in .net 4.0 (visual studio 2010)
[WebMethod]
public XmlDocument Showmethod(int rollno, params string[]DSFromaddress)
{
-- coding part--
}
After consuming the web method in .net using addwebreference, i could access this method.
for ex: webservice name webshow
webshow.webclass obj = new webshow.webclass();
XmlNode resutls;
results=obj.Showmethod(5);
i did not pass second argument as it is optional parameter in Showmethod in .net webservice.
But it shows an error No overload for method 'Showmethod' takes 1 argument.
why params optional parameter is not working.how to solve this problem?
help needed.
Thanks,
First of all "params" parameter is not an optional parameter, not even close. It is strictly mandatory, but the syntax of its use only looks like optional.
This is nothing more than the syntactic sugar for implicit creation of the array at the call:
ShowMethod(0, "1", "2");
is strictly equivalent to
ShowMethod(0, new string[] {"1", "2",});
And yet, you can write
// works for me: ShowMethod(0);
which is the same as
ShowMethod(0, new string[0]); // note: not null array, but empty array
Your problem could be explained by something simple: you are showing not exactly the same code you are using; you are mixing up something, something like that. If you disagree, please write a simple self-contained code sample manifesting the problem. First of all, it will help you to sort out things by yourself. If not, show this code in your question.
—SA
这篇关于使用webservice后,'params'可选参数在.net应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!