0

I have a StringBuilder named "sb", and I want to make a timer update a TextBox named "StatusBox"'s text to match sb.ToString. My current code, however, throws the following error:

Error BC30451: 'sb' is not declared. It may be inaccessible due to its protection level.

The current code is below:

Imports System.Net
Imports System.Text
Imports System.IO
Imports System.IO.Compression

Public Class MainForm
Public Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim DefaultLocation As New Point(489, 424)
    Dim CheckingLocation As New Point(438, 424)
    Dim ErrorLocation As New Point(319, 424)
    StatusLabel.Location = DefaultLocation
    StatusBox.Hide()

    If CheckForInternetConnection() = False Then
        StatusBox.Hide()
        StatusLabel.Show()
        StatusLabel.Text = "Either the AppVeyor is down, or you have no internet connectivity."
        StatusLabel.Location = ErrorLocation
        StatusBar.Image = RPCS3_Updater.My.Resources._Error
    Else
        StatusLabel.Text = "Checking for updates..."
        StatusLabel.Location = CheckingLocation
        StatusBar.Image = RPCS3_Updater.My.Resources.Checking
        StatusBox.Show()
        StatusLabel.Hide()
        Dim appPath As String = Application.StartupPath()
        Dim sb = New StringBuilder()
        Dim psi = New ProcessStartInfo() With
        {
                .WorkingDirectory = appPath,
                .FileName = appPath.ToString + "\Resources\GetRPCS3.bat",
                .CreateNoWindow = True,
                .RedirectStandardOutput = True,
                .RedirectStandardInput = True,
                .UseShellExecute = False
        }
        Dim pSpawn = New Process()
        pSpawn.StartInfo = psi
        AddHandler pSpawn.OutputDataReceived, Function(senderA, args) sb.AppendLine(args.Data)
        pSpawn.Start()
        pSpawn.BeginOutputReadLine()
        StatusBox.Text = sb.ToString()
        StatusTimer.Enabled = True
        pSpawn.WaitForExit()
        StatusBox.Text = sb.ToString()
        If pSpawn.HasExited Then
            Dim appPath1 As String = Application.StartupPath()
            Dim zipPath As String = Directory.GetFiles(appPath1 + "\Resources").First.ToString
            Dim extractPath As String = ""
            ZipFile.ExtractToDirectory(zipPath, appPath1 + "\Resources\RPCS3")
        End If
    End If
End Sub

Public Shared Function CheckForInternetConnection() As Boolean
    Try
        Using client = New WebClient()
            Using stream = client.OpenRead("https://ci.appveyor.com/project/rpcs3/rpcs3/build/artifacts")
                Return True
            End Using
        End Using
    Catch
        Return False
    End Try
End Function

Public Sub StatusTimer_Tick(sender As Object, e As EventArgs) Handles StatusTimer.Tick
    StatusBox.Text = sb.ToString()
End Sub
End Class
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • You declared (Dim) sb in form load so that is the only place it exists – Ňɏssa Pøngjǣrdenlarp Jul 19 '16 at 15:53
  • I'm aware of that, but how can I fix this without copy-pasting all that code into the timer, because if I do, then the scripts will repeat as well, and that isn't what I want. – Emil Sayahi Jul 19 '16 at 16:07
  • quoting the linked dupe: `Declared at the form level, it has form/class level scope. You can now reference myEquip anywhere in the form.` – Ňɏssa Pøngjǣrdenlarp Jul 19 '16 at 16:08
  • So, I should just move the deceleration out of the sub and into the class? – Emil Sayahi Jul 19 '16 at 16:10
  • You should strive to understand the concepts behind the code you write. That timer is not needed - the contents of the stringbuilder are set once and once only - when the form loads. It wont change as the timer ticks. But yes - ***declared*** at the form level and ***created*** (`New`) the first time you use it (form load?) - it will be available in the timer event. – Ňɏssa Pøngjǣrdenlarp Jul 19 '16 at 16:16

0 Answers0