变量'fs'隐藏了封闭块中的变量

时间:2014-02-20 16:16:27

标签: vb.net visual-studio-2010 xna

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

    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'隐藏变量。

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

2 个答案:

答案 0 :(得分: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 :(得分:2)

您不需要Dim fs...行 - Using声明涵盖声明。

Using语句本身应该没问题,但是如果你想确定输入,那么将其改为:

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