变量“fs”将变量隐藏在封闭块中

Cha*_*art 3 vb.net xna visual-studio-2010

我目前正在使用以下代码:

    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
Run Code Online (Sandbox Code Playgroud)

然而,使用后的 fs 带有蓝色下划线,并给出错误变量“fs”隐藏变量在封闭块中。

有人知道我该如何解决这个问题吗?

Dou*_*bin 5

您正在声明变量,然后在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
Run Code Online (Sandbox Code Playgroud)