2016-03-18 16 views
-4

我不斷收到上述警告ordersubmittedlastfillquantity,但一切似乎工作正常,當我打印他們似乎正確更新的變量。永遠不會分配給,並將始終有其默認值0

public partial class Form1 : Form 
{ 
    private bool ordersubmitted = false; 
    private int lastfillquantity; 

    private void SubmitOrder() 
    { 
     int lastfillquantity = e.filled; 
     ordersubmitted = true 
    } 
} 
+1

的可能的複製[現場XXX永遠不會分配給,永遠有它的默認值零(http://stackoverflow.com/questions/4811155/field-xxx-is -never-assigned-to-and-will-always-have-its-default-value-null) – CompuChip

回答

3

您沒有設置類屬性的值,而是在方法內部創建一個新屬性並將新值分配給該屬性。試試這個:

lastfillquantity
public partial class Form1 : Form 
{ 
    private bool ordersubmitted = false; 
    private int lastfillquantity; 

    private void SubmitOrder() 
    { 
     // here you need to assign it, instead of defining another class property 
     lastfillquantity = e.filled; 
     ordersubmitted = true 
    } 
} 
2

它工作不正常。正如它所說的,你永遠不會爲它設置一個價值。您只需在方法中定義另一個具有相同名稱的變量並設置其值,然後將其丟棄。類字段永遠不會獲得除零以外的任何其他值。

+2

這是一個class *字段*。變量在類級別上不存在。 – Amy

+0

@Amy好點 –

1

變量從不更新,因爲你重新聲明lastfillquantitySubmitOrder方法和更新新的變量(其中隱藏外一個)值。相反,你應該更新外部變量。

嘗試以下

private void SubmitOrder() 
{ 
    lastfillquantity = e.filled; 
    ordersubmitted = true 
} 
相關問題