0

I have searched through StackOverflow and Google for the conversion, but unfortunately I was unable to get the solution. Everything was the opposite of what I wanted. i.e., Conversion of an int[] to List.

Problem

I have this code in my [WebMethod].

[WebMethod]
public int MyMethodWS(int N, List<int> M)
{
}

And now I have a Console Application, that references this service by using this URL:

http://localhost:61090/MyMethod.asmx?WSDL

I have this code in my Console Application:

int N;
List<int> M = new List<int>();
// Some crazy user input coding.
// Ultimately you will have non-empty M list and N int.
MyMethod.MyMethodWS DA = new MyMethod.MyMethodWS();
Sum = DA.MyMethodWS(N, M);

When I run this code, I am getting this error:

#1: The best overloaded method match for 'MyMethod.MyMethod.MyMethodWS(int, int[])' has some invalid arguments.

#2: Argument 2: cannot convert from 'System.Collections.Generic.List<int>' to 'int[]'

Questions

  1. I have used only List there. But why is it trying to convert as int[]?
  2. Currently the WebService as well as Console Application, both run in the same Solution. Is it a problem? Should I use different solutions, or different instances of Visual Studio 2012 altogether?
0

1 Answer 1

2

It is trying to convert it to int[], because MyMethodWS takes a int[] as a parameter, instead of List<int> that you are trying to pass it. To convert a List<int> to int[], call List<T>.ToArray();

So change the line Sum = DA.MyMethodWS(N, M); to Sum = DA.MyMethodWS(N, M.ToArray());

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

8 Comments

What makes you say it takes an int[] parameter? Could you explain why this is the case, even though it's defined as List<int>?
@JeroenVannevel Actually I'm a bit confused now too. I think originally the method that Praveen was trying to call was named DivisorSumWS or something, and not that MyMethodWS which clearly takes a List<int> as a parameter. However, that error message clearly states that the method takes int[] as a parameter... I'm not quite sure whats going on, maybe Praveen changed the original post or I read it incorrectly to begin with.
@JaakkoLipsanen Please update your answer. Yes, I changed it because well, it is the code from my production, which I am not supposed to put. :)
The thing is, I have declared as List<int>, but it expects in int[]. But your solution worked removing the errors, but VS 2012 Crashed.
@PraveenKumar Do you still have a problem or error in your code? Because based on the edited code from your question, it should work jus fine. If it's still not working, try to clean&rebuild and if even that doesn't help, post more information so that we can help you.
|

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.