所以我收到錯誤「對象引用未設置爲對象的實例」。當我運行我的程序。我試圖用多態性產生我的結果,但是這個錯誤在我和我的結局之間。有人能幫助我理解這一點嗎?什麼是:對象引用未設置爲對象的實例
Module PublicationTester
Sub Main()
Dim publications(4) As Publication
Dim book1 As New Book("Booktitle1", "Publisherb1", "100.00", "Myself",
"b1b1b1b1", "11/11/11")
Dim book2 As New Book("Booktitle2", "Publisherb2", "100.00", "Myself",
"b2b2b2b2", "11/11/11")
Dim mag1 As New Magazine("Magtitle1", "Publisherm1", "100.00", "Myself",
"issue1", "1")
Dim mag2 As New Magazine("Magtitle2", "Publisherm2", "100.00", "Myself",
"issue2", "2")
publications(1) = book1
publications(2) = book2
publications(3) = mag1
publications(4) = mag2
For Each publication In publications
***Console.WriteLine(publication.ToString())***
Next
'Console.WriteLine(publications(1))
'Console.WriteLine(publications(2))
'Console.WriteLine(publications(3))
'Console.WriteLine(publications(4))
'Console.Read()
End Sub
End Module
Public MustInherit Class Publication
Private titleValue As String
Private publisherValue As String
Private priceValue As Decimal
Public Sub New(ByVal title As String, ByVal publisher As String,
ByVal price As String)
titleValue = title
publisherValue = publisher
priceValue = price
End Sub
Public Property titleVal() As String
Get
Return titleValue
End Get
Set(title As String)
titleValue = title
End Set
End Property
Public Property publisherVal() As String
Get
Return publisherValue
End Get
Set(publisher As String)
publisherValue = publisher
End Set
End Property
Public Property priceVal() As String
Get
Return priceValue
End Get
Set(price As String)
priceValue = price
End Set
End Property
Public Overrides Function ToString() As String
Return "Title: " & titleValue & vbCrLf &
"Publisher: " & publisherValue & vbCrLf &
"Price: " & priceValue & vbCrLf
End Function
End Class
Public Class Book
Inherits Publication
Private authorValue As String
Private ISBNValue As String
Private publishDateValue As String
Public Sub New(ByVal title As String, ByVal publisher As String,
ByVal price As String, author As String, ISBN As String,
publishDate As String)
MyBase.New(title, publisher, price)
authorValue = author
ISBNValue = ISBN
publishDateValue = publishDate
End Sub
Public Property authorVal() As String
Get
Return authorValue
End Get
Set(author As String)
authorValue = author
End Set
End Property
Public Property ISBNVal() As String
Get
Return ISBNValue
End Get
Set(ISBN As String)
ISBNValue = ISBN
End Set
End Property
Public Property publishDateVal() As String
Get
Return publishDateValue
End Get
Set(publishDate As String)
publishDateValue = publishDate
End Set
End Property
Public Overrides Function ToString() As String
Return MyBase.ToString() &
"Author: " & authorValue & vbCrLf &
"ISBN: " & ISBNValue & vbCrLf &
"Publish Date: " & publishDateValue & vbCrLf
End Function
End Class
Public Class Magazine
Inherits Publication
Private editorValue As String
Private issueValue As String
Private frequencyValue As Decimal
Public Sub New(ByVal title As String, ByVal publisher As String,
ByVal price As String, editor As String,
issue As String, frequency As String)
MyBase.New(title, publisher, price)
editorValue = editor
issueValue = issue
frequencyValue = frequency
End Sub
Public Property editorVal() As String
Get
Return editorValue
End Get
Set(edit As String)
editorValue = edit
End Set
End Property
Public Property issueVal() As String
Get
Return issueValue
End Get
Set(issue As String)
issueValue = issue
End Set
End Property
Public Property frequencyVal() As String
Get
Return frequencyValue
End Get
Set(frequency As String)
frequencyValue = frequency
End Set
End Property
Public Overrides Function ToString() As String
Return MyBase.ToString() &
"Editor: " & editorValue & vbCrLf &
"Issue: " & issueValue & vbCrLf &
"Frequency: " & frequencyValue & vbCrLf
End Function
End Class
您會在哪一行發生錯誤? – Steve
for循環「Console.Writeline(publication.tostring())」 – SkyVar
您重寫了ToString方法嗎?如果是,你可以顯示它的代碼? – Steve