2014-01-11 120 views
-1

所以我收到错误“对象引用未设置为对象的实例”。当我运行我的程序。我试图用多态性产生我的结果,但是这个错误在我和我的结局之间。有人能帮助我理解这一点吗?什么是:对象引用未设置为对象的实例

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

您会在哪一行发生错误? – Steve

+0

for循环“Console.Writeline(publication.tostring())” – SkyVar

+0

您重写了ToString方法吗?如果是,你可以显示它的代码? – Steve

回答

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 

.... 
+0

那有用,我看过那个小小的因素。任何人都希望在请改变 昏暗出版物(4)公开 到 昏暗出版物(3)如公开 和实现上面的例子。 – SkyVar

+0

感谢您的帮助和理解。 – SkyVar

相关问题