2014-01-11 117 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

相關問題