2014-02-12 34 views
-3

我有這種情況下,如果在if和else if塊中聲明相同的變量。問題在於if語句被繞過,執行移到else if語句,然後不允許重新分配變量。這是一個簡單的例子。如果,否則如果變量不更新

if (filename.ToLower().Contains(".tif")) 
{ 
    int test = 0; 
    string test1; 
} 
else if (ValidExtension(filename.ToLower())) 
{ 
    int test = 1; 
    //test still equals 0? Not possible right? 
    string test1 = "hello"; 
    //test1 still equals null? Again not possible..? 
    //EDIT: 
    //Code that accesses test1 here, but test1 = null unless renamed to anything but test1... 
} 

ValidExtension只是檢查「文件名」的擴展名是否爲任何其他圖像格式,但「.tif」。它返回true或false。如果它很重要,則此代碼在GUI線程以外的其他線程上執行。

任何人都可以解釋爲什麼會發生這種可能的原因或如何發生?

編輯:

我完全理解,這些範圍內的變量我很清楚微軟的文檔。在我的編碼生涯中,我已經完成了許多其他時間,並且它工作正常。我只是有一個奇怪的例子,因爲某些原因,如果我在其下的某個代碼中使用string test1 =「hello」,仍然在Else If塊內,它會返回爲空。我對微軟文檔的理解是:test1 =「hello」,test1應該總是=「hello」,但不是,它仍然等於null。但是,如果我將代碼更改爲test2 =「hello」,那麼它實際上就是「hello」。我的問題可能措辭不佳,顯然不是很清楚。這並不意味着是一個noob問題,我想知道這將是什麼樣的錯誤呢?

編輯:

這裏是正在運行的實際代碼..希望這將提供更好的上下文的問題..

if (filename.ToLower().Contains(".tif")) 
{ 
    using (BaseBL.BeginImpersonate()) 
    { 
     string output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder, string.Format("{0}¡{1}¡{2}", "FileDrop", BaseBL.ApplicationUser, filename)); 
     var cpyCount = 1; 
     while (File.Exists(output)) 
     { 
      output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder, 
                 string.Format("{0}¡{1}¡{2}{3}.tif", "FileDrop", BaseBL.ApplicationUser, filename.Replace(".tif", string.Empty), 
                    cpyCount)); 
      cpyCount++; 
     } 
     var f = File.Create(output); 
     f.Write(file.ToArray(), 0, int.Parse(file.Length.ToString())); 
     f.Close(); 
     f.Dispose(); 
    } 
} 
else if (ValidExtension(filename.ToLower())) 
{ 
    var image = new Bitmap(file); 
    string output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder, string.Format("{0}¡{1}¡{2}.tif", "FileDrop", BaseBL.ApplicationUser, Path.GetFileNameWithoutExtension(filename))); 
    //output = null always. If changed to output1 it contains the valid filepath...?? 
    var cpyCount = 1; 
    while (File.Exists(output)) 
    { 
     output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder, 
                string.Format("{0}¡{1}¡{2}{3}.tif", "FileDrop", BaseBL.ApplicationUser, Path.GetFileNameWithoutExtension(filename), 
                   cpyCount)); 
     cpyCount++; 
    } 
    using (BaseBL.BeginImpersonate()) 
     image.Save(output, ImageFormat.Tiff); 
    image.Dispose(); 
} 
+7

您的變量僅在範圍'{...}'它們被定義可見。 – Habib

+4

您不適合觀察程序,而不是程序不正確。 – Servy

+1

@Bathsheba好吧,考慮到幾分鐘內有4個答案,而且在我看來,他們每個人都錯誤地解釋了這個問題,這意味着問題根本不明確。這是降低它的可接受的理由。 – Servy

回答

2

您需要聲明前的「測試」變量如果聲明。該代碼看起來像:

int test = 0; 
string test1; 
if (filename.ToLower().Contains(".tif")) 
{ 
    test1 = null; 
} 
else if (ValidExtension(filename.ToLower())) 
{ 
    test = 1; 
    test1 = "hello"; 
} 

祝你好運!

注意 - 您必須這樣做的原因是因爲C#中的作用域。如果你在一個循環中聲明一個變量,或者(基本上)每次使用花括號時,它只存在於該區域內。這意味着您的原始代碼實際上有兩個測試變量 - 每個變量都是唯一的。

+2

這是怎麼解釋他在將'test'設置爲'0'後立即觀察'test' '1'?閱讀您複製到您自己的答案中的代碼註釋。 – Servy

7

我有這種情況,其中相同的變量在if和else if塊中聲明。

這是不可能的 - 這些都是兩個不同的變量在不同範圍相同。我懷疑你如何觀察這些變量是不恰當的。

相同的變量他們需要外被宣佈爲if

int test; 
string test1; 
if (filename.ToLower().Contains(".tif")) 
{ 
    test = 0; 
} 
else if (ValidExtension(filename.ToLower())) 
{ 
    test = 1; 
    test1 = "hello"; 
} 
+0

我完全同意你的觀點,這不可能是我在這裏提出問題的原因。在什麼樣的可能世界中,這種行爲才能被觀察 – agritton

+0

_你在觀察'test'嗎?在調試器中?您是否確定調試器正在查看正確的實例? –

+0

我在test = 1上設置了一個斷點;跨過它,它的門檻等於0 ...如果我在它下方的控制檯打印,它打印0.? – agritton

2

我剛剛發現這個微軟的一些網頁:

如果要聲明一個塊中的變量構造如If語句,該變量的作用域只是直到塊的結尾。終身是直到程序結束。

Variable and Method Scope in Microsoft .NET

enter image description here

Another link

+1

這不是-1值得,只是可能不值+1。 -1是有害答案,這絕對不是。它甚至引用MSDN。 – Magus

+0

這絕對有用。它正確地解釋了爲什麼這個變量不能按預期工作,使用官方來源。 – Magus

+1

嘿,我只是新來的,但是-1?我只是把MS頁面和所有的屏幕截圖..哦!那麼這是一個新手 –

相關問題