2015-07-12 81 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