局部变量未定义 - VB.Net

时间:2016-10-09 09:20:16

标签: vb.net variables

 Imports System.IO
 Public Class ReadAndInterpret
'File dialog part
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    OpenFileDialog1.ShowDialog()
    TextBox1.Text = OpenFileDialog1.FileName
End Sub
'Set up the interpretator
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim ClowsReader As New StreamReader(TextBox1.Text)

    Do While ClowsReader.Peek() <> -1
        Dim TextLine As String
        TextLine = ClowsReader.ReadLine() & vbNewLine
        'The function/variable interpreter part
        If TextLine.Contains("var") Then


            Dim CheckVarName As String = TextLine.Split(" ")(1)
            Dim CheckVarVal As String = TextLine.Split(CheckVarName & "=")(1)


        ElseIf TextLine.Contains("MsgBox") Then


        Dim CheckLine As String = TextLine.Split(">")(1)
            MsgBox(CheckLine)

            If CheckLine.Contains(CheckVarName) Then
                MsgBox(CheckVarName)
            End If


        End If

    Loop
End Sub
End Class

所以这是我在VB.NET中编写的脚本语言解释器的代码。只有MsgBox和变量函数(尚未完成。)在此代码中,它给出了一个未定义CheckVarName的错误,即使它被定义了

 Dim CheckVarName As String = TextLine.Split(" ")(1)
代码中的

有谁知道为什么说CheckVarName没有定义? (它给出了这段代码中的错误):

        If CheckLine.Contains(CheckVarName) Then
             MsgBox(CheckVarName)
         End If

1 个答案:

答案 0 :(得分:0)

只需要在类的顶部声明您的变量,如下所示:

Public Class ReadAndInterpret
    Dim CheckVarName As String = TextLine.Split(" ")(1)
    ...

希望它能为你自己带来好运!