Im trying to get the sum of a list from a web service.
[WebMethod]
public string CalculateSum(List<int> listInt)
{
int[] sum = listInt.ToArray();
return sum; // error under sum on this line
}
But I get the error cannot convert int to string, this should work?
Client Code:
public partial class Form1 : Form
{
List<int> listInt = new List<int>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
listInt.Add(Convert.ToInt32(textBox1.Text));
textBox1.Clear();
listBox1.Items.Clear();
for (int i = 0; i < listInt.Count; i++)
{
listBox1.Items.Add(listInt[i]);
}
}
private void button2_Click(object sender, EventArgs e)
{
CalculateSumOfList.ServiceReference1.Service1SoapClient client = new CalculateSumOfList.ServiceReference1.Service1SoapClient();
CalculateSumOfList.ServiceReference1.ArrayOfInt arrayOfInt = new CalculateSumOfList.ServiceReference1.ArrayOfInt();
arrayOfInt.AddRange(listInt);
int result = client.CalculateSum(arrayOfInt); //here
label1.Text = Convert.ToString(result);
}
}
}
I get errors with client.CalculateSum(arrayOfInt);
You can use either
or
But do consider first: Should a function that sums int values really return a string? It seems to make much more sense as an
intfunction: