vb.net 变量“fs”在封闭块中隐藏变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21913476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:50:50  来源:igfitidea点击:

Variable 'fs' hides variable in an enclosing block

vb.netvisual-studio-2010xna

提问by Charlie Stuart

I am currently using the following code:

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

    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

However the fs after using is underlined in blue and gives the error Variable 'fs' hides variable in an enclosing block.

但是,使用后的 fs 用蓝色下划线标出,并给出错误 Variable 'fs' hides variable in a enclosure block。

Does anybody know how i can fix this?

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

采纳答案by Douglas Barbin

You are declaring the variable, then using that same variable name in a Usingblock (which tries to declare it again).

您正在声明变量,然后在Using块中使用相同的变量名(它试图再次声明它)。

Change it to this:

改成这样:

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

回答by Jon Egerton

You don't need the Dim fs...line - the Usingstatement covers the declaration.

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

The Usingstatement on its own should be fine as you have it, but if you want to be sure of the typing then change it to:

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

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