Click here to Skip to main content
15,883,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,
Posted

1 solution

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:
C#
ShowMethod(0, "1", "2");

is strictly equivalent to
C#
ShowMethod(0, new string[] {"1", "2",});

And yet, you can write
C#
// works for me:
ShowMethod(0);

which is the same as
C#
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
 
Share this answer
 
v2
Comments
christhuxavier 27-Jun-14 12:48pm    
I am sorry.. but if you use params keyworkd in noraml application. it did not show any error see below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
public int test(int i,params string [] s)
{
return i;
}
}
class t : Program
{
static void Main(string[] args)
{
t obj = new t();
int li = obj.test(4);
Console.WriteLine();
Console.ReadLine();
}
}
}

can you help me how it works? and i want to use optinal parameter in my .net xml webservice method. how to use optional paramter. kindly suggest me
Sergey Alexandrovich Kryukov 27-Jun-14 23:24pm    
Correct. That is what I am talking about. You know how it should work. No, try to find out how your working code is different. It could be just typo or other mess-up of this sort. You think that you are using this class or interface, but you are using another one, with simple name, different namespace. Something like that. Just be accurate. Such features of Visual Studio as "Get To Definition" and "Find All References" will help you.
—SA
Sergey Alexandrovich Kryukov 27-Jun-14 23:25pm    
Now, help with what? Explain what? I already explained, but you don't want to read it carefully: this is not an optional parameter.
—SA
christhuxavier 28-Jun-14 0:09am    
i understood by your explaining params is not a optional paramter. i asked what else way to give optional paramter in web method..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900