2017-10-11 71 views
0

我的項目是預定義一個記錄數組,然後使用以下語法將它們輸出到高中的列表框中syntaxt here但是它輸出錯誤「System.Exception:」延遲當「聯繫人」是延遲表達式的結果時,賦值給值類型爲'Contact'的字段無效''使用數組的記錄

我試過了很多故障排除並找不到問題 - 可能是真的很簡單。我會appriciate任何幫助,你們可以提供。

公共類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 

末級

回答

0

變化setuprecords到

Sub setuprecords(ByRef addressbook As Contact()) 

雖然這只是ByRef addressbook,它被默認爲輸入Object

Option Strict On作爲代碼的第一行,它會在您忘記指定必要類型時通知您。

你會看到

Sub displayoutput(ByVal addressbook) 

也需要同樣的關注。

+0

謝謝djv! 這解決了我的問題馬上。我的老師從來沒有教過我們這件事 –