VB.net,引用对不同私有子对象中的私有子对象中的公共变量所做的更改

时间:2019-06-22 14:32:57

标签: arrays vb.net multidimensional-array text-files streamreader

在程序的一个私有子目录(openFile_Click)中,我对在Public类中变暗的变量(即名称,距离和点)进行了更改。但是,当我尝试从另一个私有子目录(btnGameEnter)重印它们时,它们为空白。我知道在测试之后,变量在openFile子中已正确设置,并且不知道为什么在btnGameEnter子中,在openFile子中完成的相同过程(将变量添加到列表框)在btnGameEnter子子中不起作用已经有来自openFile的数据。

Public Class frmdetails


    Dim Names(100) As String
    Dim Distance(100, 2) As String
    Dim Points(100, 2) As String

    Private Sub openFile_Click(sender As Object, e As EventArgs) Handles openFile.Click
        OpenFileDialog.ShowDialog()
        Dim strFileName = OpenFileDialog.FileName
        Dim objReader As New System.IO.StreamReader(strFileName)
        Dim textline As String


        lstNames.Items.Clear()
        lstDistance.Items.Clear()
        lstPoints.Items.Clear()

        Dim Count As Integer = 0


        Do While objReader.Peek() <> -1

            textline = objReader.ReadLine() & vbNewLine

            Dim parts() As String = textline.Split("|")

            Names(Count) = parts(0)
            Distance(Count, 0) = parts(1)
            Distance(Count, 1) = parts(2)
            Distance(Count, 2) = parts(3)
            Points(Count, 0) = parts(4)
            Points(Count, 1) = parts(5)
            Points(Count, 2) = parts(6)

            Count = Count + 1


        Loop

        For n = 0 To Count - 1
            lstNames.Items.Add(Names(n))
            lstNames.Items.Add(" ")
            lstDistance.Items.Add(Distance(n, 0) + " Miles")
            lstDistance.Items.Add(" ")
            lstPoints.Items.Add(Points(n, 0))
            lstPoints.Items.Add(" ")
        Next

    End Sub

    Private Sub btnGameEnter_Click(sender As Object, e As EventArgs) Handles btnGameEnter.Click

        Dim ChosenGame As Integer = 0
        Dim Count As Integer = 0

        ChosenGame = cboWhichGame.Text

        lstGameNum.Items.Clear()

        lstNames.Items.Clear()
        lstDistance.Items.Clear()
        lstPoints.Items.Clear()

        lstGameNum.Items.Add(ChosenGame)


        For n = 0 To Count - 1
            lstNames.Items.Add(Names(n))
            lstNames.Items.Add(" ")
            lstDistance.Items.Add(Distance(n, (ChosenGame - 1)) + " Miles")
            lstDistance.Items.Add(" ")
            lstPoints.Items.Add(Points(n, (ChosenGame - 1)))
            lstPoints.Items.Add(" ")
        Next

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我发现了这些问题:

  1. (1 ...)中,将局部变量初始化为(cdr (assq 'ace-window THELIST))。它的价值永远不变。这意味着for循环将永远不会执行。使其成为一个类字段,而不是在所有Subs之间共享它(并从两个Subs中删除ace-window)。

    BtnGameEnter_Click
  2. 您应将Dim Count As Integer = 0初始化为Dim CountPublic Class frmReferencingFields Dim Count As Integer = 0 .... ,其中第一个游戏为ChosenGame。因此,加法时请勿减去1。这样一来,您就可以将语音名称添加到Integer中,而不是一个空号码。

    cboWhichGame.SelectedIndex

此外,我不确定您是否为距离和点选择了正确的类型。他们真的是琴弦吗?


我正在提议另一种方法。名称,距离和点代表位置吗?它假设。因此,让我们创建一个0

ComboBox

您似乎有3场比赛。因此,让我们以如下形式创建一个游戏数组

Dim ChosenGame As Integer = cboWhichGame.SelectedIndex
...
lstDistance.Items.Add(Distance(n, ChosenGame) + " Miles")
lstPoints.Items.Add(Points(n, ChosenGame))

游戏数量是固定的,这取决于文件格式。每个游戏包含不同数量的位置。最好为此使用Location,因为它会自动增长以适应内容。

我们还要添加一个方便的例程,将游戏添加到列表中。我们可以在两个Subs中重复使用它。

Public Class Location
    Property Name As String
    Property Distance As Double
    Property Points As Integer
End Class

请注意,我们如何用一个变量Dim game(2) As List(Of Location) 和一个位置为List(Of T)来表示游戏,而不必处理名称,距离和点的单独数组。另外,由于列表会自动跟踪计数,因此不需要单独的Private Sub DisplayGame(game As List(Of Location)) lstNames.Items.Clear() lstDistance.Items.Clear() lstPoints.Items.Clear() For Each location As Location In game lstNames.Items.Add(location.Name) lstNames.Items.Add(" ") lstDistance.Items.Add(location.Distance & " Miles") lstDistance.Items.Add(" ") lstPoints.Items.Add(location.Points.ToString()) lstPoints.Items.Add(" ") Next End Sub 变量。

现在,两个Subs变成

game As List(Of Location)

location As Location