无法在VB2010中使用.txt文件填充结构数组的变量

时间:2014-05-20 06:24:37

标签: arrays vb.net data-structures

这只是这个项目的第一种形式,我从头开始无数次地进行罐装和重建。我在我的智慧结束,似乎无法使程序运行没有错误。我正在使用split方法将数组中的数据指定为结构中的name和ID#变量,以便稍后我可以使用其他算法搜索它们 你可以看到数组实际上正在填充第二张图片截图中的.txt文件的内容.... (由于必须点击cntrl来截取屏幕,因此它非常透明)

但是,我不断收到NUll.reference异常或类似的废话 HELP !!!!!

......太棒了它不会让我放照片,因为我是新的......精彩

Public Class Form1

    Structure records
        Dim fullName As String
        Dim stuID As String
    End Structure

    Dim stuRec() As records

    'Enables the last name text box
    Private Sub txtfname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtfname.TextChanged

        txtlname.Enabled = True
    End Sub

    'Enables the student ID text box
    Private Sub txtlname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtlname.TextChanged

        txtstuID.Enabled = True
    End Sub

    'Enables the submit button
    Private Sub txtstuID_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtstuID.Enter

        btnSub.Enabled = True
    End Sub

    'Displays an error message when invalid data is entered into the student id masked text box
    Private Sub txtstuID_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles txtstuID.MaskInputRejected

        MessageBox.Show("Please Enter the Last 7 Digits of your Student ID", "Invalid Input")
    End Sub

    'This loads the 1-dimension array stuRec() of the structure records with the data in the text file stuID.txt
    'While setting the data to fullName or stuID variables using the line.Split method
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim records() As String = IO.File.ReadAllLines("stuID.txt")

        For i As Integer = 0 To records.Count - 1
            Dim data() As String = records(i).Split(","c)
            stuRec(i).fullName = data(0)   ...Here's where the problem is...
            stuRec(i).stuID = data(1)
        Next
    End Sub

    Private Sub btnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSub.Click

        Dim fname As String = CStr(txtfname.Text)
        Dim lname As String = CStr(txtlname.Text)
        Dim fullName As String = (fname & " " & lname)

        ‘(The Search code will go here)
    End Sub
  

块引用

我不知道为什么知识分子坚持使用这个功能......它自己添加它来解决我遇到的问题但它没有解决问题

    Private Function data(ByVal p1 As Integer) As String
        Throw New NotImplementedException
    End Function
End Class

1 个答案:

答案 0 :(得分:0)

   Structure records
        Dim fullName As String
        Dim stuID As String
    End Structure

    Dim stuRec() As records

stuRec数组的长度未定义,我建议使用List(记录)。

Dim stuRec as New List(of records)

向列表添加条目:

stuRec.Add(New records with {.fullName = data(0), .stuID = data(1)})

如果检查数据是否为ex,那就更好了。在添加之前长度为2,因此如果缺少某些数据,则不会出现另一个异常