2012-03-05 2 views
0

Im пытается получить сумму списка из веб-сервиса.webmethod int to string по сумме списка

[WebMethod] 
public string CalculateSum(List<int> listInt) 
{ 
     int[] sum = listInt.ToArray(); 

     return sum; // error under sum on this line 

} 

Но я получаю ошибку, не могу преобразовать int в строку, это должно работать?

Client Код:

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); 


     } 



    } 
} 

я получаю ошибки с client.CalculateSum(arrayOfInt);

ответ

1

Вы можете использовать либо

return sum.ToString(); 

или

return "" + sum; 

Но рассмотрим первый: Если функция, которая суммирует значения INT действительно возвращает строку? Похоже, что это имеет смысл в качестве функции int:

public int CalculateSum(List<int> listInt) 
{ 
    int sum = listInt.Sum(); 
    return sum; // now matches the return-type of the method 
} 
5
[WebMethod] 
public int CalculateSum(List<int> listInt) 
{ 
    int sum = listInt.Sum(); 
    return sum; // error under sum on this line 

} 

Обратите внимание на возвращаемое значение вашей функции - вы объявляя он возвращает строку, а не целое число.