我已經繼承了(看看那個,一個雙關語,抱歉...)一套VB.Net應用程序,它們共享共同的功能,目前「複製和粘貼」複製到所有這些應用程序中。我想開始重構過程。附註:我主要是C#開發人員,不太熟悉VB。如何從WindowsFormsApplicationBase繼承?
所有這些VB應用程序都使用「應用程序框架」。我試圖做的第一件事就是讓它們都從一個通用的應用程序基類繼承。我創建了一個新的類:
Namespace My
Public Class ParentApplicationBase
Inherits Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
Public Sub Bar()
Console.WriteLine("Here we are in Bar()")
End Sub
End Class
End Namespace
然後,在ApplicationEvents.vb的,ChildApp.vb和Application.Designer.vb,我改變了
Partial Friend MyApplication
到
Partial Friend MyApplication : Inherits ParentApplicationBase
然而,導致錯誤:
Base class 'ParentApplicationBase' specified for class 'MyApplication' cannot be different from the base class 'Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase' of one of its other partial types.
我有雙倍和三倍檢查,我所有的部分類都確實繼承自ParentApplicationBase。它看起來好像在應用程序框架的魔力中還有其他「隱藏」的地方,它使得它認爲基類是WindowsFormsApplicationBase而不是ParentApplicationBase。
我已經搜索了一般網頁和Stackoverflow都是徒勞的。我發現最近的是這個問題VB.NET - How do you change the namespace/class names for a "Windows application" from the default My.MyApplication?,但這並沒有幫助。
在此先感謝您的任何建議!
當您在多個部分聲明中劃分類的定義時,編譯器將該類視爲所有部分聲明的聯合。這不僅適用於成員,還適用於實現,繼承和訪問級別。一個類可以實現多個接口,但它不能從多個基類繼承。因此,所有的Inherits語句都必須指定相同的基類。您可以決定哪個類應該是您的部分類的基類,並從其部分聲明中刪除指定不同基類的任何Inherits語句。 – Codexer
要了解更多信息,請參閱** https://msdn.microsoft.com/en-us/library/8d17t7cd.aspx**。 – Codexer