2012-03-05 14 views
0

私は、Webサービスからリストの合計を取得しようとしています。webmethod intのリストの合計の文字列

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

     return sum; // error under sum on this line 

} 

しかし、私はエラーが文字列にint型に変換できません、これは動作するはずですか?

クライアントコード:

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

: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 

} 

お知らせあなたの関数の戻り値でエラーが発生します - あなたはそれが文字列ではなく整数を返すと宣言しています。

あなたが

return sum.ToString(); 

または

return "" + sum; 

のいずれかを使用することができますしかし、最初に考えるん