2015-04-29 18 views
-2

我在製作一個計算3D打印成本的程序。我提出這個問題。無法在2個整數中隱式地將類型'int'轉換爲'string'

private void button1_Click(object sender, EventArgs e) 
    { 
     double getal1 = Int32.Parse(textBox1.Text); 
     double getal2 = Int32.Parse(textBox2.Text); 
     double volume = 0.002405281875; 
     double volume1 = 0; 
     int dichtheidpla = Int32.Parse("1,24"); 


     if (comboBox1.Text == "PLA") 
     { 
      //volume berekenen 
      volume1 = volume * getal1; 
      V.Text = volume1.ToString("N2"); 

      //gewicht berekenen 


      int volumeberekend = Convert.ToInt32(volume1); 
      gewicht1.Text = volumeberekend * dichtheidpla; 

     } 
     else if (comboBox1.Text == "ABS") 
     { 

     } 

    } 
} 
當我要計算gewicht1錯誤

顯示我:不能隱式轉換類型「詮釋」到「串」。 'volumeberekend * dichtheidpla'下面有一條紅線。

+2

1,24看起來並不像一個int。 – eckes

+0

對於某些文化(如fr-FR),它是。 –

+0

您正在執行數學計算,將兩個「int」類型相乘並嘗試將結果存儲在「字符串」中。 C#是一種相當強類型的語言,您不能在沒有轉換的情況下混合和匹配類型。 – sab669

回答

1

gewicht1.Text是一個字符串類型,表達式的結果將是一個int。所以你只需要將結果轉換成string

gewicht1.Text = (volumeberekend * dichtheidpla).ToString(); 
+0

當我嘗試運行它時,我得到這一行上的錯誤: int dichtheidpla = Int32.Parse(「1,24」); FormatException未處理。 –

+0

@ThijnLinders,因爲'1,24'不是一個整數。刪除'',它會完美運行 – Shaharyar

+0

但我怎麼能得到1,24呢? –

0

它,因爲它說,它不能隱式轉換intstring沒有你決定要如何這樣的事情發生。

試試這個顯式的轉換,而不是:

int volumeberekend = Convert.ToInt32(volume1); 
int total = volumeberekend * dichtheidpla 
gewicht1.Text = total.ToString(); 
相關問題