将项目添加到阵列(VB 2008)

时间:2018-12-22 18:14:52

标签: arrays vb.net

该程序的目标是使用StreamReader解释文件中的曲棍球统计信息,然后显示添加的点列。下面的代码有点这样做,但是在没有将点值添加到数组的意义上,它是无效的,而是单独输出它。寻找有关如何将点值合并到aryTextFile();

的帮助
    Dim hockeyFile, LineOfText, aryTextFile() As String
    Dim i As Integer
    Dim nameText(), NumberText(), goalsText(), assistsText(), GamesWonText() As String
    Dim IntAssists(), IntGoals(), PointsText() As Single

    hockeyFile = "C:\Users\Bob\Downloads\hockey.txt" 'state location of file

    Dim objReader As New System.IO.StreamReader(hockeyFile) 'objReader can read hockeyFile


    For i = 0 To objReader.Peek() <> -1 'reads each line seperately, ends when there is no more data to read
        LineOfText = objReader.ReadLine 'stores seperate lines of data in HockeyFile into LineofText  
        aryTextFile = LineOfText.Split(",") 'takes lines and converts data into array
        Name = aryTextFile(0) 'first piece of data in lines of text is the name 
        nameText(i) = aryTextFile(0)
        If nameText(0) = "Name" Then
            TextBox1.Text = LineOfText & ", Points." & vbCrLf 'displays first line fo text and adds "Points" label
        End If

        If Name <> "Name" Then 'when second line starts, then begin to intepret data
            NumberText(i) = aryTextFile(1)
            assistsText(i) = aryTextFile(2) 'assists are in third value of array 
            goalsText(i) = aryTextFile(3) 'goals are in fourth value of array
            GamesWonText(i) = aryTextFile(4)

            IntAssists(i) = Val(assistsText(i)) 'since our assists value is a string by default, it must be converted to a integer

            IntGoals(i) = Val(goalsText(i)) 'since our goals value is a string by default, it must be converted to a integer

            PointsText(i) = (IntGoals(i) * 2) + (IntAssists(i)) 'goals are two points, assists are one point


            TextBox1.Text = TextBox1.Text & NumberText(i) & assistsText(i) & goalsText(i) & GamesWonText(i) & PointsText(i) & vbCrLf 'Displays points as last value in each line

        End If
    Next i

3 个答案:

答案 0 :(得分:1)

这应该使您更接近:

它需要额外的验证。它没有考虑名称和目标之间的价值。

Private Sub ProcessHockeyStats()

    Try

        Dim inputFile As String = "c:\temp\hockey.txt"
        Dim outputFile As String = "c:\temp\output.txt"

        If Not File.Exists(inputFile) Then
            MessageBox.Show("Missing input file")
            Return
        End If

        If File.Exists(outputFile) Then
            File.Delete(outputFile)
        End If

        Dim lines() As String = File.ReadAllLines(inputFile)

        Dim output As List(Of String) = New List(Of String)

        Dim firstLine As Boolean = True

        For Each line As String In lines

            Dim values() As String = line.Split(","c)
            Dim points As Integer

            If firstLine Then
                output.Add("Name, Assists, Goals, Points")
                firstLine = False
            Else
                'needs validation for values
                points = CInt(values(1) * 2) + CInt(values(2))
                output.Add(String.Concat(line, ",", points))
            End If

        Next

        File.WriteAllLines("c:\temp\outfile.txt", output)

    Catch ex As Exception
        MessageBox.Show(String.Concat("Error occurred: ", ex.Message))
    End Try

End Sub

答案 1 :(得分:1)

VS2008是较早的,尤其是在免费的更高版本的Visual Studio中时。我感觉想要展示使用最新代码的实现。与其他人一样,我强烈支持为此建立一个类。区别在于,我的班级更加聪明,使用Factory模式创建实例,并使用Property根据需要计算Points

Public Class HockeyPlayer
    Public Property Name As String
    Public Property Number As String
    Public Property Assists As Integer
    Public Property Goals As Integer
    Public Property Wins As Integer

    Public ReadOnly Property Points As Integer
        Get
           Return (Goals * 2) + Assists
        End Get
    End Property

    Public Shared Function FromCSVLine(line As String) As HockeyPlayer
         Dim parts() As String = line.Split(",")
         Return New HockeyPlayer With {
              .Name = parts(0),
              .Number = parts(1),
              .Assists = CInt(parts(2)),
              .Goals = CInt(parts(3)),
              .Wins = CInt(parts(4))
         }
    End Function
End Class

Dim hockeyFile As String = "C:\Users\Bob\Downloads\hockey.txt"

Dim players = File.ReadLines(hockeyFile).Skip(1).
             Select(Function(line) HockeyPlayer.FromCSVLine(line)).
             ToList() 'ToList() is optional, but I included it since you asked about an array

Dim result As New StringBuilder("Name, Number, Assists, Goals, Wins, Points")
For Each player In players
      result.AppendLine($"{player.Name}, {player.Number}, {player.Assists}, {player.Goals}, {player.Wins}, {player.Points}")
Next player

TextBox1.Text = result.ToString()

我本来会给你VS 2008版本的,但是从这个角度来看,这里甚至到VS 2010都无法做到的唯一事情就是字符串插值……您应该升级。

答案 2 :(得分:0)

并行数组实际上不是处理此问题的方法。创建一个类或结构来组织数据。然后创建该类的列表。该列表可以设置为DataSource的{​​{1}},它将在漂亮的列中显示您的数据,其标题与Hockey类中的属性名称匹配。您可以通过Hockey的任何属性轻松地在HockeyList中对数据进行排序。

DataGridView