2014-02-20 30 views
1

我目前使用以下代碼:變量「FS」隱藏變量在封閉塊

Public Sub CreateScore() 
    ' open isolated storage, and write the savefile. 
    Dim fs As IsolatedStorageFileStream = Nothing 
    Using fs = savegameStorage.CreateFile("Score") 
    If fs IsNot Nothing Then 

     ' just overwrite the existing info for this example. 
     Dim bytes As Byte() = System.BitConverter.GetBytes(Scorecount) 
     fs.Write(bytes, 0, bytes.Length) 
    End If 

    End Using 

End Sub 

然而,使用後的FS在藍色用下劃線表示,並給出了誤差變量「FS」隱藏變量在一個封閉的塊。

有誰知道我該如何解決這個問題?

+1

嘗試'使用FS作爲IsolatedStorageFileStream = savegameStorage.CreateFile( 「分數」)'和擺脫DIM語句。你有2個版本的FS,但我不確定ISO是如何工作的。 – Plutonix

+0

Using語句將在到達塊的末尾時處理該變量。但是,既然您之前已經聲明過,該塊不能這樣做。您需要在Using語句本身中將該變量聲明爲Plutonix和Jon Egerton所示。 fs變量只會被限制在Using塊中,因此可以自動處理。 –

回答

3

您在聲明變量,然後使用相同的Using塊中的變量名(它試圖再次聲明它)。

它改成這樣:

Public Sub CreateScore() 
    ' open isolated storage, and write the savefile. 
    Using fs As IsolateStorageFileStream = savegameStorage.CreateFile("Score") 
    If fs IsNot Nothing Then 

     ' just overwrite the existing info for this example. 
     Dim bytes As Byte() = System.BitConverter.GetBytes(Scorecount) 
     fs.Write(bytes, 0, bytes.Length) 
    End If 

    End Using 

End Sub 
1

您不需要Dim fs...行 - Using聲明涵蓋聲明。

自身的Using的說法應該是罰款,你擁有了它,但如果你想確保打字的,然後將其更改爲:

Using fs As IsolatedStorageFileStream = savegameStorage.CreateFile("Score") 
... 
+0

'使用'聲明可能會或可能不會像他擁有的那樣好。如果他有'Option Infer Off'(我意識到這可能不太可能,但不管出於何種原因我們都在我的辦公室)。 –

+0

實際上,在「Option Infer Off」的時候,他仍然(很好):'fs'最終會被輸入爲「Object」,儘管編碼很麻煩,但它仍然可以工作。 –