vb.net,引用其他私人潜艇中私人子中公共变量的更改



在我的程序的一个私人子(openfile_click(中,我对我在公共类中调查的变量进行更改,称为名称,距离和点。但是,当我尝试从另一个私人子(btngameenter(转载它们时,它们会空白。我知道将变量在测试后在OpenFile子中正确设置,并且不知道为什么在OpenFile子中完成的相同过程(将变量添加到listBox中(在btngameenter sub中不起作用,该变量应在其中变量。已经有来自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. BtnGameEnter_Click中,您将局部变量初始化为Dim Count As Integer = 0。它的价值永远不会改变。这意味着FO循环永远不会执行。使其成为类字段以在所有潜艇之间共享(并从两个subs中删除Dim Count(。

    Public Class frmReferencingFields
        Dim Count As Integer = 0
        ....
    
  2. 您应该初始化ChosenGame,它是带有cboWhichGame.SelectedIndexInteger,其中第一个游戏将为0。因此,添加时不要减去1。这使您可以将口语名称添加到ComboBox而不是裸机。

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

另外,我不确定您是否为距离和点选择了正确的类型。他们真的是字符串吗?


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

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

您似乎有3场比赛。因此,让我们以形式创建这样的游戏阵列

Dim game(2) As List(Of Location)

游戏的数量已固定,因为它取决于文件格式。每个游戏都包含可变数量的位置。最好将List(Of T)用于此过程,因为它会自动生长以适合内容。

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

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

请注意,我们如何用单个变量game As List(Of Location)和作为location As Location的位置表示游戏,而不必处理单独的名称,距离和点的数组。另外,由于列表会自动跟踪计数,因此无需单独的Count变量。

现在,两个潜艇变成

Private Sub OpenFile_Click(sender As Object, e As EventArgs) Handles openFile.Click
    OpenFileDialog.ShowDialog()
    Dim strFileName = OpenFileDialog.FileName
    'Create the lists
    For i As Integer = 0 To UBound(game)
        game(i) = New List(Of Location)()
    Next i
    For Each textline As String In File.ReadLines(strFileName)
        Dim parts() As String = textline.Split("|"c)
        If parts.Length = 7 Then 'Not an empty line
            For g As Integer = 0 To UBound(game)
                Dim location = New Location()
                location.Name = parts(0)
                location.Distance = CDbl(parts(1 + g))
                location.Points = CInt(parts(4 + g))
                game(g).Add(location)
            Next g
        End If
    Next
    DisplayGame(game(0))
End Sub

Private Sub BtnGameEnter_Click(sender As Object, e As EventArgs) Handles btnGameEnter.Click
    Dim ChosenGame As Integer = cboWhichGame.SelectedIndex
    lstGameNum.Items.Clear()
    lstGameNum.Items.Add(ChosenGame)
    DisplayGame(game(ChosenGame))
End Sub

相关内容

  • 没有找到相关文章

最新更新