BC30451 'VARIABLE' is not declared. It may be inaccessible due to its protection level

TheDeibo picture TheDeibo · Nov 6, 2016 · Viewed 17.1k times · Source

I seem to have an error with the code below.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.CenterToScreen()


    If My.Computer.FileSystem.FileExists("bin\php\php.exe") Then
        Dim PHPRC As String = ""
        Dim PHP_BINARY As String = "bin\php\php.exe"
    Else
        Dim PHP_BINARY As String = "php"
    End If

    If My.Computer.FileSystem.FileExists("PocketMine-MP.phar") Then
        Dim POCKETMINE_FILE As String = "PocketMine-MP.phar"
    Else
        If My.Computer.FileSystem.FileExists("src\pocketmine\PocketMine.php") Then
            Dim POCKETMINE_FILE As String = "src\pocketmine\PocketMine.php"
        Else
            MsgBox("Couldn't find a valid PocketMine-MP installation", MsgBoxStyle.Exclamation, "PocketMine-MP")
        End If

    End If

    Process.Start("C:\Users\Damian\Desktop\Games\Pocketmine\Installer\PocketMine-MP\bin\mintty.exe", "-o Columns=88 -o Rows=32 -o AllowBlinking=0 -o FontQuality=3 -o Font='DejaVu Sans Mono' -o FontHeight=10 -o CursorType=0 -o CursorBlinks=1 -h error -t 'PocketMine-MP' -i bin/pocketmine.ico -w max" & PHP_BINARY & "" & POCKETMINE_FILE & "" & "--enable-ansi")

End Sub

I keep on getting this error

BC30451 'PHP_BINARY' is not declared. It may be inaccessible due to its protection level.

BC30451 'POCKETMINE_FILE' is not declared. It may be inaccessible due to its protection level.

What am I doing wrong?

(FYI, Its in Form1_Load just for test reasons.)

Answer

soohoonigan picture soohoonigan · Nov 6, 2016

You're dimming two variables within if statements, so once you hit the "end if" your variables are gone, or "out of scope". You should definitely do some research on variable scope... To fix this in your code, first declare the strings up top, inside the sub but outside of your if statements. Then just use the if statements to change what the variables hold; that way, when your process call comes up, the variables will not be out of scope:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.CenterToScreen()

    Dim PHP_BINARY As String = Nothing
    Dim POCKETMINE_FILE As String = Nothing

    If My.Computer.FileSystem.FileExists("bin\php\php.exe") Then
        PHP_BINARY = "bin\php\php.exe"
    Else
        PHP_BINARY = "php"
    End If

    If My.Computer.FileSystem.FileExists("PocketMine-MP.phar") Then
        POCKETMINE_FILE = "PocketMine-MP.phar"
    Else
        If My.Computer.FileSystem.FileExists("src\pocketmine\PocketMine.php") Then
            POCKETMINE_FILE = "src\pocketmine\PocketMine.php"
        Else
            MsgBox("Couldn't find a valid PocketMine-MP installation", MsgBoxStyle.Exclamation, "PocketMine-MP")
        End If

    End If

    Process.Start("C:\Users\Damian\Desktop\Games\Pocketmine\Installer\PocketMine-MP\bin\mintty.exe", "-o Columns=88 -o Rows=32 -o AllowBlinking=0 -o FontQuality=3 -o Font='DejaVu Sans Mono' -o FontHeight=10 -o CursorType=0 -o CursorBlinks=1 -h error -t 'PocketMine-MP' -i bin/pocketmine.ico -w max" & PHP_BINARY & "" & POCKETMINE_FILE & "" & "--enable-ansi")

End Sub