0

所以,我有以下 VB 代码:

Try
        Using verSR As New StreamReader(appDataVersionLoc)
            Dim txtVern As String
            txtVern = verSR.ReadToEnd()
        End Using
    Catch ex As Exception
        Dim verFile As System.IO.FileStream
        verFile = System.IO.File.Create(appDataVersionLoc)
        My.Computer.FileSystem.WriteAllText("appDataVersionLoc", "0.0.0.0", True)
        MessageBox.Show("Version file missing/corrupt, created a new one.")
    End Try

看到那个名为“txtVern”的变量了吗?我正在尝试在 Try-End Try 块之外的代码中的其他地方使用它。差不多就是这样:

Try
        Using verSR As New StreamReader(appDataVersionLoc)
            Dim txtVern As String
            txtVern = verSR.ReadToEnd()
        End Using
    Catch ex As Exception
        Dim verFile As System.IO.FileStream
        verFile = System.IO.File.Create(appDataVersionLoc)
        My.Computer.FileSystem.WriteAllText("appDataVersionLoc", "0.0.0.0", True)
        MessageBox.Show("Version file missing/corrupt, created a new one.")
    End Try

    blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah

    Dim currentversion As String = txtVern

现在,我是一个完整的 VB 菜鸟,我收到了这个错误:

'txtVern' 未声明。由于其保护级别,它可能无法访问。

我怎样才能解决这个问题?谢谢!

4

2 回答 2

2

在任何块内声明的变量仅在该块内可见。Try您必须在阻止之前移动声明:

Dim txtVern As String = String.Empty
Try
    Using verSR As New StreamReader(appDataVersionLoc)
        txtVern = verSR.ReadToEnd()
    End Using
Catch ex As Exception
    Dim verFile As System.IO.FileStream
    verFile = System.IO.File.Create(appDataVersionLoc)
    My.Computer.FileSystem.WriteAllText("appDataVersionLoc", "0.0.0.0", True)
    MessageBox.Show("Version file missing/corrupt, created a new one.")
End Try

blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah

Dim currentversion As String = txtVern
于 2013-03-31T06:41:59.383 回答
0

在 try 块中声明变量 txtVern 时,您将它们的范围限制在 try 块中。尝试在子例程的开头声明它们。

于 2013-03-31T06:43:09.623 回答