变量未声明;由于其保护级别,它可能无法访问

时间:2011-07-11 23:25:30

标签: vb.net

今天我决定想出一个在VB.net中对我有用的程序(我以前从未编写过VB.net)。到目前为止一切都很顺利,但我遇到了上面提到的错误。问题是在获取配置文件注释下的windowssevenexistsonsource布尔值。我也会接受任何代码批评,因为我想在开始之前摆脱不良做法! (子确实结束但我没有包含该代码)

代码:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    'Check that the entries required are not empty
    If String.IsNullOrEmpty(sourceipaddress.Text) Or String.IsNullOrEmpty(destinationipaddress.Text) Then
        MsgBox("Source or destination IP address is empty")
        Exit Sub
    End If

    'First we need to establish the operating system of the source and destination
    Try
        Dim windowssevenexistsonsource As Boolean = IO.Directory.Exists("\\" & sourceipaddress.Text & "\c$\users")
        Dim windowssevenexistsondestination As Boolean = IO.Directory.Exists("\\" & sourceipaddress.Text & "\c$\users")
    Catch ex As Exception
        MsgBox(ex.Message)
        Exit Sub
    End Try


    'Now we need to get a list of profiles in the relevant directory and put them into an array
    'Declare variables and empty the array
    Dim Sourcedirectorylistarray, destinationdirectorylistarray As String()
    Dim sourcedirectoryentry, destinationdirectoryentry As String
    Dim Sourcerootpath, destinationrootpath As String

    'Get List of Profiles
    Try
        If windowssevenexistsonsource = True Then
            Sourcedirectorylistarray = System.IO.Directory.GetDirectories("\\" & sourceipaddress.Text & "\c$\users\")
            destinationdirectorylistarray = System.IO.Directory.GetDirectories("\\" & destinationipaddress.Text & "\c$\users\")
        Else
            MsgBox("test")
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
        Exit Sub
    End Try

1 个答案:

答案 0 :(得分:1)

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