2015-04-26 25 views
1

我試圖計算一些屬性,但我不知道它是如何如何。 proporties是string類型,我知道string值無法計算。所以我雖然關於使他們int。將字符串轉換爲本地int來計算

public class ItemProperties 
      { 
       public string Item { get; set; } 
       public string Description { get; set; } 
       public string Quantity { get; set; } 
       public string UnitPrice { get; set; } 
       public string Tax { get; set; } 

       public int TotalPay { get { return Quantity * UnitPrice; } set; } 
       //Now I get error on the line above because string can't calculate 

      } 

操作員「*」不能應用於類型「字符串」和 「串」

的操作數所以基本上我需要在stringQuantityUnitPrice當地int變量轉換和然後用它們來計算。誰能幫我嗎?

UPDATE:

下面是屬性分配給string[]

var lines = File.ReadAllLines(openFileDialog.FileName); 
for (var i = 9; i + 2 < lines.Length; i += 6) 
      { 
       Items.Add(new ItemProperties { 
        Item = lines[i], 
        Description = lines[i + 1], 
        Quantity = lines[i + 2], 
        UnitPrice = lines[i + 3], 
        Tax = lines[i + 4] 
       }); 
      } 
      itemlst.ItemsSource = Items; 
+1

你爲什麼不能做數量和單價整數? public int Quantity {get;組; } public int UnitPrice {get;組; } – nimeshjm

+0

因爲我將它們分配給'array of strings' varibales –

+0

爲什麼在將它們添加到數組之前將它們轉換爲字符串? –

回答

3

的快捷方式(沒有錯誤檢查)。將代碼:

public int TotalPay { get { return Convert.ToInt32(Quantity) * Convert.ToInt32(UnitPrice); } } 

更多的位健壯的版本:

public int TotalPay 
    { 
     get 
     { 
      int qty; 
      int price; 

      if (int.TryParse(Quantity, out qty) && int.TryParse(UnitPrice, out price)) 
      { 
       return qty * price; 
      } 

      return 0; 
     } 
    } 

這可以解決您當前的問題,但如果屬性保存int數據,則應將其定義爲int。

將它們分配給字符串數組的代碼應該將int屬性轉換爲字符串,而不是相反。

編輯 這會將從文件讀取的字符串屬性轉換爲int。請注意,如果文件中的值不是整數,這將導致錯誤。你應該有一些錯誤處理來迎合這一點。

public class ItemProperties 
{ 
    public string Item { get; set; } 
    public string Description { get; set; } 
    public int Quantity { get; set; } 
    public int UnitPrice { get; set; } 
    public string Tax { get; set; } 

    public int TotalPay 
    { 
     get 
     { 
      return Quantity * UnitPrice; 
     } 
    } 
} 

    var lines = File.ReadAllLines(openFileDialog.FileName); 

    for (var i = 9; i + 2 < lines.Length; i += 6) 
     { 
      Items.Add(new ItemProperties 
      { 
       Item = lines[i], 
       Description = lines[i + 1], 
       Quantity = Convert.ToInt32(lines[i + 2]), 
       UnitPrice = Convert.ToInt32(lines[i + 3]), 
       Tax = lines[i + 4] 
      }); 
     } 
     itemlst.ItemsSource = Items; 
+0

謝謝。我在那裏更新了我的問題,顯示了字符串的賦值。你能幫忙把int屬性轉換成字符串嗎? –

+0

爲什麼'Tax'仍然是'字符串','UnitPrice'是'int'?你會認爲它們是'雙單價'(或「浮動單價」)和「雙重稅」(或「浮動稅」)。 –

+0

是的,人們可以這樣認爲。然而,這些都是特定於OP域的業務規則,他必須改變它們。我只是提出他需要做出的改變來回答OP的問題。 – nimeshjm

0

如果它們表示整數數據,則應該使上述屬性爲整數。 您可以使用靜態Convert類解析字符串爲整數,例如:

public class ItemProperties 
{ 
    public string Item { get; set; } 
    public string Description { get; set; } 
    public int Quantity { get; set; } 
    public int UnitPrice { get; set; } 
    public int Tax { get; set; } 

    public int TotalPay { get { return Quantity * UnitPrice; } set; } 

} 

使用:

ItemProperties prop; 
try 
{ 
    prop.Quantity = Convert.ToInt32(stringVar); 
} 
catch(FormatException) 
{ 
    // error handling 
} 
+0

靜態在哪裏? –

+0

'System.Convert'類是靜態的:https://msdn.microsoft.com/en-us/library/system.convert%28v=vs.110%29.aspx – mcserep

+0

哦,我的意思是在你的代碼中。我雖然假設你在你提供的代碼示例中包含靜態代碼。我的錯 –