2012-11-24 27 views
0

我簡直不能讓我的頭繞着這個簡單的問題。布爾沒有被設置爲真正的C#

我有我asigning到測試的輸出一個布爾值:

// these are passed in to the function and will vary 
bool inReview = true; 
char status = 'V'; 

bool test = (inReview && status != 'M') || !inReview; 

計算結果爲:

bool test = (true && true) || !true; 

這應該是真實的 - 但調試器顯示的「值測試「是錯誤的。

當我試試這個:

bool inReview = true; 
char status = 'V'; 

bool test = false; 

if ((inReivew && status != 'M') || !inReview) 
{ 
    test = true; 
} 

它落入如果和調試器顯示的「測試」的價值是真實的。

現在,這裏是別的東西很奇怪,如果我這樣做:

bool test = (inReview && status != 'M') || !inReview; 
bool test2 = (inReview && status != 'M') || !inReview; 

步進通過與調試程序 - 測試首先是假的,test2的成爲真正的瞬間,但後來當我檢查測試,現在是真的!?

另外,如果我嘗試:

bool test = (inReview && status != 'M') || !inReview; 
if (test) 
{ 
    string s = "WTF?"; 
} 

步進通過 - 測試首先是假的,那麼它確實步入如果和值現在是true!?

+0

最後一個是非常可疑的。 'test'在該行被設置爲'_before_或_after_'後是否爲false? – David

+1

bool test =(inReview && status!='M')|| !inReview;這返回true,不要通過Statement Console.Write(test.ToString())繼續進行調試器打印測試; – Adil

+2

設置斷點** AFTER **'bool test'行! –

回答

3

當調試程序進入一行時,那行仍然需要被評估。您必須越過線路進行分配。一旦調試器位於右括號(}),應該設置變量的值。

+0

是的 - 我跨過這條線,數值保持爲假。奇怪的是,如果我做System.Diagnostics.Debug.Write(test.ToString());它打印真實,現在調試器顯示值爲真 – pauldbentley

0

您在調試器中評估哪條線?如果您只是在執行分配的行上設置斷點,那麼該值最初將是錯誤的,因爲該行的代碼尚未執行。進入下一行,你應該看到正確的結果。

+0

是的 - 我步入巢線,值保持爲假。我認爲它對調試器有點奇怪,因爲如果值是真的,它的行爲就會發生 – pauldbentley