Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将新书添加到数组Books()中_Arrays_Vb.net - Fatal编程技术网

Arrays 将新书添加到数组Books()中

Arrays 将新书添加到数组Books()中,arrays,vb.net,Arrays,Vb.net,我必须把一本新书添加到一个数组中。我使用了一个文本文件来填充数组,现在我需要重拨Preserve以向数组中添加一本新书。这是我的ReDim和For循环,还有一个函数可以实现这一点,但它不能正常工作。你知道怎么做吗 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim newBook As Book newBook

我必须把一本新书添加到一个数组中。我使用了一个文本文件来填充数组,现在我需要重拨Preserve以向数组中添加一本新书。这是我的ReDim和For循环,还有一个函数可以实现这一点,但它不能正常工作。你知道怎么做吗

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

    Dim newBook As Book
    newBook = EmptyBook()

    ReDim Preserve Books(Books.Count)

    For i As Integer = 0 To Books.Count - 1
        Books(i) = newBook
    Next

End Sub

Function EmptyBook() As Book
    Dim Answer As Book
    With Answer
        .title = txtTitle.Text
        .authorLastName = txtAuthor.Text
        .year = CInt(txtYear.Text)
        .value = CDec(txtValue.Text)
    End With
    Return Answer
End Function
请尝试以下操作:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

Dim newBook As Book
newBook = EmptyBook()

if Books is nothing then
  ReDim Books(0)
else
  ReDim Preserve Books(Books.GetUpperBound(0) + 1)
end if

Books(Books.GetUpperBound(0)) = newBook 

End Sub

显示填充数组和其他类的方式。