Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 变量';财政司司长';隐藏封闭块中的变量_Vb.net_Visual Studio 2010_Xna - Fatal编程技术网

Vb.net 变量';财政司司长';隐藏封闭块中的变量

Vb.net 变量';财政司司长';隐藏封闭块中的变量,vb.net,visual-studio-2010,xna,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

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

    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”在封闭块中隐藏变量


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

您不需要
Dim fs…
行-
Using
语句包含声明


单独使用
语句应该可以,但如果您想确保键入,请将其更改为:

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

您正在声明变量,然后在
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

尝试使用fs作为IsolatedStorageFileStream=savegameStorage.CreateFile(“Score”)并去掉DIM语句。您有两个版本的fs,但我不确定ISO是如何工作的。Using语句将在到达变量块末尾时处理该变量。但是,由于您之前已经声明了它,所以块不能这样做。您需要在Using语句本身中声明该变量,如Proputix和Jon Egerton所示。fs变量的作用域将仅限于Using块,因此可以安全地自动处理。
Using
语句可能会很好,也可能不会很好。如果他有
Option-referre-Off
(我意识到这可能不太可能,但不管出于什么原因,我们在我的办公室里把它关掉了)。事实上,有了“Option-referre-Off”,他仍然(有点好):
fs
会以“Object”的形式结束,这虽然很难编码,但应该仍然有效。