所以我收到错误“对象引用未设置为对象的实例”。当我运行我的程序。我正在尝试使用多态来产生我的结果,但这个错误介于我和我的结局之间。有人能帮助我理解这个吗?
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
答案 0 :(得分:3)
数组在VB中基于零。在VB中,您不指定数组大小,而是指定上部索引。因此,您的数组包含五个元素。你没有初始化publications(0)
。
Const N As Integer = 4
Dim publications(N - 1) As Publication '[0 .. N-1] = [0 .. 3]
...
publications(0) = book1
publications(1) = book2
publications(2) = mag1
publications(3) = mag2
....