使用记录数组有问题

时间:2017-10-11 15:59:38

标签: vb.net

我的项目是预定义一系列记录,然后使用以下语法syntaxt here将它们输出到高中的列表框中,但它会输出错误" System.Exception:&#39 ;对价值类型字段的后期绑定分配'联系人'在'联系'时无效是后期表达的结果。'"

我已经尝试了很多故障排除,无法找到问题 - 它可能非常简单。我会恭喜你们提供的任何帮助。

Public Class Form1

'Dim addressbook(8, 2) As String
'Dim number As i

Structure Contact

    Dim Forename As String
    Dim Surname As String
    Dim address As String

End Structure

Dim addressbook(8) As Contact

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    setuprecords(addressbook)
    displayoutput(addressbook)

End Sub
Sub setuprecords(ByRef addressbook)

    'For row = 0 To 8
    ' For column = 0 To 2
    ' If column = 0 Then
    ' addressbook(row, column) = InputBox("What is your name ")
    ' ElseIf column = 1 Then
    ' addressbook(row, column) = InputBox("What is your 2nd name")
    ' ElseIf column = 2 Then
    ' addressbook(row, column) = InputBox("What is your address")
    ' End If
    ' Next
    'Next


    addressbook(0).Forename = "Bart"
    addressbook(0).Surname = "Simpson"
    addressbook(0).address = "Springfeild"

    addressbook(1).Forename = "Lisa"
    addressbook(1).Surname = "Simpson"
    addressbook(1).address = "Springfeild"

    addressbook(2).Forename = "Homer"
    addressbook(2).Surname = "Simpson"
    addressbook(2).address = "Springfeild"

    addressbook(3).Forename = "Marge"
    addressbook(3).Surname = "Simpson"
    addressbook(3).address = "Springfeild"

    addressbook(4).Forename = "Santa's Little Helper"
    addressbook(4).Surname = "Simpson"
    addressbook(4).address = "Springfeild"

    addressbook(5).Forename = "Magie"
    addressbook(5).Surname = "Simpson"
    addressbook(5).address = "Springfeild"

    addressbook(6).Forename = "Milhouse"
    addressbook(6).Surname = "Something"
    addressbook(6).address = "Springfeild"

    addressbook(7).Forename = "Ned"
    addressbook(7).Surname = "Flanders"
    addressbook(7).address = "Springfeild"

    addressbook(8).Forename = "Willie"
    addressbook(8).Surname = "Something"
    addressbook(8).address = "Springfeild"

End Sub
Sub displayoutput(ByVal addressbook)
    'If number = 0 Then
    ' For row = 0 To 8
    ' ListBox1.Items.Add(addressbook(row, 0) & vbTab & (addressbook(row, 1) & vbTab & (addressbook(row, 2))))
    ' Next
    'Else
    ' ' ListBox1.Items.Add(addressbook(number - 1 & vbTab & column))
    ' ListBox1.Items.Add(addressbook(number - 1, 0) & vbTab & (addressbook(number - 1, 1) & vbTab & (addressbook(number - 1, 2))))
    'End If
    For row = 0 To 8
        ListBox1.Items.Add(addressbook(row).Forename)
        ListBox1.Items.Add(addressbook(row).Surname)
        ListBox1.Items.Add(addressbook(row).address)
    Next
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

将setuprecords更改为

Sub setuprecords(ByRef addressbook As Contact())

虽然只是ByRef addressbook,但默认输入Object

Option Strict On作为代码中的第一行,当您忘记指定必要的类型时,它会通知您。

你会看到

Sub displayoutput(ByVal addressbook)

也需要同样的关注。