简体   繁体   中英

VB.net, Referencing changes made to public variables in a private sub within a different private subs

In one private sub of my program (openFile_Click)I make changes to Variables which I Dim'd in the Public class, called Names, Distance, and Points. But When I try to reprint them from another private sub (btnGameEnter), theyre blank. I know that the variables are being set correctly within the openFile sub after testing it out, and dont know why the same process done in the openFile sub, which adds the variables to a listbox, doesnt work in the btnGameEnter sub, where the variables should already have the data from 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

I identified these problems:

  1. In BtnGameEnter_Click you initialize a local variable as Dim Count As Integer = 0 . Its value is never changed. This means that the for loop will never execute. Make it a class field instead to share it among all the Subs (and remove the Dim Count from both subs).

     Public Class frmReferencingFields Dim Count As Integer = 0 .... 
  2. You should initialize ChosenGame which is an Integer with cboWhichGame.SelectedIndex , where the first game will be 0 . Therefore don't subtract 1 when adding. This allows you to add speaking names to the ComboBox instead of a bare numbers.

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

Also, I'm not sure whether you have chosen the right types for distances and points. Are they really strings?


I am proposing another approach. Are name, distance and points representing locations? It assume it. So let's create a Location class

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

You seem to have 3 games. So let's create a game array like this in the form

Dim game(2) As List(Of Location)

The number of games is fixed, as it depends on the file format. Each game contains a variable number of locations. It's best to use a List(Of T) for this, as it grows automatically to fit the content.

Also let's add a handy routine which adds games to the lists. We can reuse it in both 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

Note how we can represent a game with a single variable game As List(Of Location) and a location as location As Location instead of having to deal with separate arrays of names, distances and points. Also, there is no need for a separate Count variable as the list automatically keeps track of the count.

Now, the two Subs become

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

and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM