2015-07-12 71 views
0

我已經下載了一個C#項目,並希望在VB.Net上工作,所以我決定將它從C#轉換爲VB.NET,並且在實現接口時遇到了一些問題。我一直在VB.NET中遇到關於如何必須具有Read-OnlyWrite-Only說明符的實現方面的錯誤。我想擺脫這個錯誤,但我不知道如何才能做到這一點。實現從C#到VB.NET的接口

我有三個文件:

  1. CustomPaintRichText.vb
  2. IUnderlineableSpellingControl.vb
  3. ISpellingControl.vb

這同樣與C#,但在C#中它工作正常,我想嘗試讓它像VB.net一樣工作。

CustomPaintRichText.vb:

Public Class CustomPaintRichText 
Inherits RichTextBox 
Implements IUnderlineableSpellingControl 

Public m_underlinedSections As Dictionary(Of Integer, Integer) 
Public m_protectedSections As Dictionary(Of Integer, Integer) 
Public m_ignoredSections As Dictionary(Of Integer, Integer) 
Public Property UnderlinedSections() As Dictionary(Of Integer, Integer) 
    Get 
     If m_underlinedSections Is Nothing Then 
      m_underlinedSections = New Dictionary(Of Integer, Integer)() 
     End If 
     Return m_underlinedSections 
    End Get 
    Set(value As Dictionary(Of Integer, Integer)) 
     m_underlinedSections = value 
    End Set 
End Property 

Public WriteOnly Property ProtectedSections() As Dictionary(Of Integer, Integer) 
    Set(value As Dictionary(Of Integer, Integer)) 
     m_protectedSections = value 
    End Set 
End Property 

Public WriteOnly Property IgnoredSections() As Dictionary(Of Integer, Integer) 
    Set(value As Dictionary(Of Integer, Integer)) 
     m_ignoredSections = value 
    End Set 
End Property 

Private spellingEnabled As Boolean 
Private spellingAutoEnabled As Boolean 
Private m_isPassWordProtected As Boolean 

Private penColour As Pen 
Public Property WhatPenColour() As Pen 
    Get 
     Return penColour 
    End Get 
    Set(value As Pen) 
     penColour = value 
    End Set 
End Property 

Public Property IsSpellingEnabled() As Boolean 
    Get 
     Return spellingEnabled 
    End Get 
    Set(value As Boolean) 
     spellingEnabled = value 
    End Set 
End Property 

Public Property IsSpellingAutoEnabled() As Boolean 
    Get 
     Return spellingAutoEnabled 
    End Get 
    Set(value As Boolean) 
     spellingAutoEnabled = value 
     If Not spellingEnabled Then 
      spellingEnabled = value 
     End If 
    End Set 
End Property 

Public Property IsPassWordProtected() As Boolean 
    Get 
     Return m_isPassWordProtected 
    End Get 
    Set(value As Boolean) 
     m_isPassWordProtected = value 
    End Set 
End Property 
End Class 

IUnderlineableSpellingControl.vb:

Public Interface IUnderlineableSpellingControl 
    Inherits ISpellingControl 
    Inherits IUnderlineable 
End Interface 

ISpellingControl.vb:

Public Interface ISpellingControl 
<Browsable(True)> _ 
Property IsSpellingEnabled() As Boolean 
Property SelectionStart() As Integer 
Property SelectionLength() As Integer 
Property SelectedText() As String 
Property Text() As String 
Property ContextMenuStrip() As ContextMenuStrip 
Property WhatPenColour() As Pen 
Property Parent() As Control 
Event Disposed As EventHandler 
Event Enter As EventHandler 
Event TextChanged As EventHandler 
Property [ReadOnly]() As Boolean 
ReadOnly Property IsPassWordProtected() As Boolean 
Sub Cut() 
Sub Copy() 
Sub Paste(clipFormat As DataFormats.Format) 
Sub [Select](start As Integer, length As Integer) 
Function Focus() As Boolean 
Sub Invalidate(invalidateChildren As Boolean) 
WriteOnly Property IgnoredSections() As Dictionary(Of Integer, Integer) 
End Interface 

如果我把旁邊Implements IUnderlineableSpellingControl光標CARRET和按回車鍵,CustomPaintRichText.vb類中,我得到:

Public Property ContextMenuStrip1 As ContextMenuStrip Implements ISpellingControl.ContextMenuStrip 

Public Sub Copy1() Implements ISpellingControl.Copy 

End Sub 

Public Sub Cut1() Implements ISpellingControl.Cut 

End Sub 

Public Event Disposed1(sender As Object, e As EventArgs) Implements ISpellingControl.Disposed 

Public Event Enter1(sender As Object, e As EventArgs) Implements ISpellingControl.Enter 

Public Function Focus1() As Boolean Implements ISpellingControl.Focus 

End Function 

Public WriteOnly Property IgnoredSections1 As Dictionary(Of Integer, Integer) Implements ISpellingControl.IgnoredSections 
    Set(value As Dictionary(Of Integer, Integer)) 

    End Set 
End Property 

Public Sub Invalidate1(invalidateChildren As Boolean) Implements ISpellingControl.Invalidate 

End Sub 

Public ReadOnly Property IsPassWordProtected1 As Boolean Implements ISpellingControl.IsPassWordProtected 
    Get 

    End Get 
End Property 

Public Property IsSpellingEnabled1 As Boolean Implements ISpellingControl.IsSpellingEnabled 

Public Property Parent1 As Control Implements ISpellingControl.Parent 

Public Sub Paste1(clipFormat As DataFormats.Format) Implements ISpellingControl.Paste 

End Sub 

Public Property ReadOnly1 As Boolean Implements ISpellingControl.ReadOnly 

Public Sub Select1(start As Integer, length As Integer) Implements ISpellingControl.Select 

End Sub 

Public Property SelectedText1 As String Implements ISpellingControl.SelectedText 

Public Property SelectionLength1 As Integer Implements ISpellingControl.SelectionLength 

Public Property SelectionStart1 As Integer Implements ISpellingControl.SelectionStart 

Public Property Text1 As String Implements ISpellingControl.Text 

Public Event TextChanged1(sender As Object, e As EventArgs) Implements ISpellingControl.TextChanged 

Public Property WhatPenColour1 As Pen Implements ISpellingControl.WhatPenColour 

Public Sub CustomPaint1() Implements IUnderlineable.CustomPaint 

End Sub 

Public Property IsSpellingAutoEnabled1 As Boolean Implements IUnderlineable.IsSpellingAutoEnabled 

Public Event KeyDown1(sender As Object, e As KeyEventArgs) Implements IUnderlineable.KeyDown 

Public WriteOnly Property ProtectedSections1 As Dictionary(Of Integer, Integer) Implements IUnderlineable.ProtectedSections 
    Set(value As Dictionary(Of Integer, Integer)) 

    End Set 
End Property 

Public Sub RemoveWordFromUnderliningList1(wordStart As Integer) Implements IUnderlineable.RemoveWordFromUnderliningList 

End Sub 

Public Event SelectionChanged1(sender As Object, e As EventArgs) Implements IUnderlineable.SelectionChanged 

Public Property UnderlinedSections1 As Dictionary(Of Integer, Integer) Implements IUnderlineable.UnderlinedSections 

當我從形式進行更改CustomPaintRichText,我將有額外的附加控制並最終沒有用。

錯誤Implements IUnderlineableSpellingControl。它強調說:'CustomPaintRichText'必須實現接口'ISpellingControl'的'Event Disposed(sender As Object,e As System.EventArgs)'。這是30個錯誤以及..必須執行的界面之一。

這裏是error list如果你想看看我得到什麼樣的錯誤。

下面是的.cs文件的情況下:

  1. CustomPaintRichText.cs
  2. IUnderlineableSpellingControl.cs
  3. ISpellingControl.cs
+2

哪裏在那巨大的代碼牆是錯誤?接口說明符錯誤應該是編譯器錯誤,但「沒有任何工作」聽起來像它編譯。 – Plutonix

+0

Option Explicit on看看它是否編譯? –

+2

一個'Interface'不是一個緊箍咒 - 有上述任何方法或屬性機構的任何代碼,所以他們沒有做任何事情,這可以解釋爲什麼「實在不行」 – Plutonix

回答

2

恭喜你,你讓漢斯帕桑特去「啊!」 :)

雖然說到他的觀點,但在.NET世界中通常會鼓勵混合和匹配從VB,C#,C++/CLI,F#或其他任何語言編譯的程序集,並且這是您的問題的實際解決方案。但是,如果你堅持把這個C#項目轉換爲VB的等價物,那麼需要理解接口在這兩種語言之間的差異。

C#有兩種類型的執行情況:隱性和顯性(見http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx)。 VB只有一個明確的樣式,與C#不一樣(見https://msdn.microsoft.com/en-us/library/28e2e18x.aspx)。

所有這些「必須實現」的錯誤意味着他們說的很多:你必須在子類的適當成員上使用Implements關鍵字,因爲VB不會執行接口的隱式實現。這是一個C#的東西。當你擊中旁邊Implements IUnderlineableSpellingControl光標插入符Enter鍵,IDE生成受影響(顯然失蹤)的成員模板代碼,完成與Implements。這樣做是爲了幫助您,但在這種情況下,您必須查看代碼並將Implements子句放在需要的地方(並且可能會刪除該模板代碼)。

C#有這樣它就會自動「線式」實現你的類和接口之間的匹配成員名稱正在實施一個整潔的隱式樣式。如果有多個接口具有相同的成員(具有相同的簽名),那麼它們將全部與班級中的同一成員一起實施(請參閱http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx)。視情況而定,這可能是一件好事或不太好的事情。

C#有一個明確的有限的風格。一個簡單地以InterfaceName.MemberName的格式定義名稱中的成員(請參閱https://msdn.microsoft.com/en-us/library/ms173157.aspx)。

VB只有它的顯式樣式,但它允許Implements子句中的接口成員列表,以便它們全部由類中同一個成員實現。這是用於連接多個接口的C#隱式實現的解決方法。

最後,有一些消息來源聲稱VB不能在已經實現的超類的子類上重新實現接口(例如http://www.dotnetheaven.com/article/how-to-re-implement-interfaces-in-vb.net)。我不知道這是否是真的,但我可以斷定VS 2012的VB和更高版本允許這樣的重新實現。

+0

我想我應該得到額外的代表我做的漢斯帕贊特做的。我得出了一個結論,我應該堅持C#,但我很欣賞徹底的解釋。 – Zer0

0

C#允許你實現一個只寫或只讀屬性與讀/寫性能。 (VB 2015也允許這個)。 您可以輕鬆地解決此之前,2015年VB - 這裏是爲您IsPasswordProtected上實施的實例:

Private ReadOnly Property ISpellingControl_IsPassWordProtected() As Boolean Implements ISpellingControl.IsPassWordProtected 
    Get 
     Return IsPassWordProtected 
    End Get 
End Property 
Public Property IsPassWordProtected() As Boolean 
    Get 
     Return isPassWordProtected_Renamed 
    End Get 
    Set(ByVal value As Boolean) 
     isPassWordProtected_Renamed = value 
    End Set 
End Property 

的「器具」雲上調用現有的讀/寫性能的新的只讀屬性。

+0

感謝戴夫,我希望你沒有花太多時間來回答這個問題。我會保留這個作爲未來的參考。 – Zer0

+0

沒問題 - 但不要被這個論壇的其他人嚇倒。關於在VB中實現接口的問題只是一個複雜的C#到VB轉換問題 - 在這個論壇中,人們對轉換問題的容忍度很小。 –

+0

謝謝你讓我知道:) – Zer0