开发者

Variable is not declared; it may be inaccessible due to its protection level

Today I decided to come up with a program that would be useful for me in VB.net (I have never coded in VB.net before). All is going fine up till this point but I have hit a snag with the error mentioned above. The problem is with the windowssevenexistsonsource boolean under the get get of profiles comment. I will also take any code criticism well as I would like to get out of bad practices before I start! (the sub does end but I have not included that code)

Code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVa开发者_开发问答l 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


In declaring your variables windowssevenexitsonsource and windowssevenexistsondestination in your try block you are limiting their scope to the try block. Try declaring them at the beginning of your subroutine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜