从C#到VB.NET的实现接口 [英] Implementing Interface from C# to VB.NET

查看:63
本文介绍了从C#到VB.NET的实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了一个C#项目并想在VB.Net上工作,所以我决定将其从C#转换为VB.NET,在实现接口时遇到了一些问题.我在VB.NET中不断收到有关我必须如何具有 Read-Only Write-Only 说明符的实现的错误.我想摆脱这个错误,但我不知道该如何实现.

我有三个文件:

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

C#也是如此,但是在C#中它可以正常工作,我想尝试使其完全像VB.net中的那样工作.

CustomPaintRichText.vb:

 公共类CustomPaintRichText继承RichTextBox实现IUnderlineableSpellingControl作为字典的公共m_underlinedSections(整数,整数)作为字典的公共m_protectedSections(整数,整数)作为字典的公共m_ignoredSections(整数,整数)公共属性UnderlinedSections()作为字典(Integer,Integer)得到如果m_underlinedSections没有了,那么m_underlinedSections =新字典(Integer,Integer)()万一返回m_underlinedSections结束获取Set(值作为Dictionary(Of Integer,Integer))m_underlinedSections =值端套最终财产字典的公共WriteOnly属性ProtectedSections()(整数,整数)Set(值作为Dictionary(Of Integer,Integer))m_protectedSections =值端套最终财产字典的公共WriteOnly属性IgnoredSections()(整数,整数)Set(值作为Dictionary(Of Integer,Integer))m_ignoredSections =值端套最终财产私有拼写启用为布尔值私人拼写自动启用为布尔值私有m_isPassWordProtected为布尔值私人笔颜色作为笔公共财产WhatPenColour()作为笔得到返还笔颜色结束获取设置(值作为笔)penColour =值端套最终财产公共属性IsSpellingEnabled()为布尔值得到返回拼写已启用结束获取设置(值作为布尔值)spellingEnabled =值端套最终财产公共属性IsSpellingAutoEnabled()作为Boolean得到返回拼写自动启用结束获取设置(值作为布尔值)spellingAutoEnabled =值如果未启用拼写,则spellingEnabled =值万一端套最终财产公共属性IsPassWordProtected()作为Boolean得到返回m_isPassWordProtected结束获取设置(值作为布尔值)m_isPassWordProtected =值端套最终财产末级 

IUnderlineableSpellingControl.vb:

 公共接口IUnderlineableSpellingControl继承ISpellingControl继承IUnderlineable终端接口 

ISpellingControl.vb:

 公共接口ISpellingControl< Browsable(True)>_属性IsSpellingEnabled()为布尔值属性SelectionStart()作为整数属性SelectionLength()作为整数属性SelectedText()作为字符串属性Text()作为字符串属性ContextMenuStrip()作为ContextMenuStrip属性WhatPenColour()作为笔属性Parent()作为控件作为EventHandler处置的事件事件输入为EventHandler事件TextChanged为EventHandler属性[ReadOnly]()为布尔值ReadOnly属性IsPassWordProtected()为布尔值子剪切()子Copy()子粘贴(clipFormat为DataFormats.Format)子[选择](以整数开始,以整数开始长度)函数Focus()为布尔值子无效(invalidateChildren为布尔型)WriteOnly属性IgnoredSections()作为字典(Integer,Integer)终端接口 

如果我将光标carret保留在 Implements IUnderlineableSpellingControl 旁边,然后按Enter键,则在CustomPaintRichText.vb类中,我得到:

 公共属性ContextMenuStrip1,因为ContextMenuStrip实现ISpellingControl.ContextMenuStrip公共子Copy1()实现ISpellingControl.Copy结束子公共子Cut1()实现ISpellingControl.Cut结束子公共事件Disposed1(发送者作为对象,e作为EventArgs)实现ISpellingControl.Disposed公共事件Enter1(发送者作为对象,e作为EventArgs)实现ISpellingControl.Enter公共函数Focus1()为Boolean实现ISpellingControl.Focus结束功能字典(Integer,Integer)的公共WriteOnly属性IgnoredSections1实现ISpellingControl.IgnoredSectionsSet(值作为Dictionary(Of Integer,Integer))端套最终财产公共子Invalidate1(作为布尔值的invalidateChildren)实现ISpellingControl.Invalidate结束子公共只读属性IsPassWordProtected1,因为布尔实现ISpellingControl.IsPassWordProtected得到结束获取最终财产公共属性IsSpellingEnabled1为Boolean实现ISpellingControl.IsSpellingEnabled公共属性Parent1作为控件实现ISpellingControl.Parent公共子Paste1(clipFormat为DataFormats.Format)实现ISpellingControl.Paste结束子公共属性ReadOnly1为Boolean实现ISpellingControl.ReadOnlyPublic Sub Select1(以Integer开头,以Integer长度开头)实现ISpellingControl.Select结束子公共属性SelectedText1作为字符串实现ISpellingControl.SelectedText作为整数实现ISpellingControl.SelectionLength的公共属性SelectionLength1公共属性SelectionStart1,因为Integer实现ISpellingControl.SelectionStart公共属性Text1作为字符串实现ISpellingControl.Text公共事件TextChanged1(发送者作为对象,e作为EventArgs)实现ISpellingControl.TextChanged公共财产WhatPenColour1作为Pen实现ISpellingControl.WhatPenColour公共子CustomPaint1()实现IUnderlineable.CustomPaint结束子公共属性IsSpellingAutoEnabled1为Boolean实现IUnderlineable.IsSpellingAutoEnabled公共事件KeyDown1(发送者作为对象,e作为KeyEventArgs)实现IUnderlineable.KeyDown字典(Integer,Integer)的公共WriteOnly属性ProtectedSections1实现IUnderlineable.ProtectedSectionsSet(值作为Dictionary(Of Integer,Integer))端套最终财产公共子RemoveWordFromUnderliningList1(wordStart作为整数)实现IUnderlineable.RemoveWordFromUnderliningList结束子公共事件SelectionChanged1(发送者作为对象,e作为EventArgs)实现IUnderlineable.SelectionChanged字典(Integer,Integer)的公共属性UnderlinedSections1实现IUnderlineable.UnderlinedSections 

当我从表单对CustomPaintRichText进行更改时,我将获得其他额外的控件,最终无济于事.

错误位于 IUnderlineableSpellingControl 的实现中.强调指出:"CustomPaintRichText"必须为接口"ISpellingControl"实现"Event Disposed(发送者作为对象,e作为System.EventArgs)".这是30.错误以及..must实现..for接口的错误之一.

如果要查看我遇到的错误类型,请参见错误列表.

以下是.cs文件,以防万一:

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

解决方案

恭喜,您使Hans Passant赢得了"U!":)

不过,说到他的观点,在.NET世界中通常鼓励混合和匹配从VB,C#,C ++/CLI,F#或其他任何语言编译的程序集,这是解决问题的实用方法.但是,如果您坚持要将此C#项目转换为等效的VB,则需要了解这两种语言之间接口实现方式的差异.

C#具有两种实现方式:隐式和显式(请参见https://msdn.microsoft.com/zh-CN/library/28e2e18x.aspx ).

所有这些必须实现"的错误几乎意味着他们所说的话:您必须在子类的相应成员上使用 Implements 关键字,因为VB不会执行接口的隐式实现.那是C#的事情.当您在 Implements IUnderlineableSpellingControl 旁边的光标插入符号处按ENTER键时,IDE会为受影响的(显然缺少)成员生成模板代码,并带有 Implements .这样做是为了帮助您,但在这种情况下,您必须查看代码,并在需要的地方放入 Implements 子句(并可能摆脱该模板代码)./p>

C#具有简洁的隐式样式,它将通过在类和要实现的接口之间匹配成员名称来自动连接"实现.如果具有相同成员(具有相同签名)的接口不止一个,则将在您的类中使用相同成员来实现它们(请参见 https://msdn.microsoft.com/zh-CN/library/ms173157.aspx ).

VB仅具有其显式样式,但它允许在 Implements 子句中使用接口成员的列表,以便它们都由类中的同一成员实现.这是连接多个接口的C#隐式实现的解决方法.

最后,有一些消息来源声称VB无法在已经实现它的超类的子类上重新实现接口(例如,error list if you want to see what kind of errors I'm getting.

Here are the .cs files in case:

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

解决方案

Congratulations, you made Hans Passant go "Ugh!" :)

Speaking to his point, though, mixing and matching assemblies compiled from VB, C#, C++/CLI, F#, or whatever, is generally encouraged in the .NET world, and is the practical solution to your problem. However, if you insist on transforming this C# project into its VB equivalent, one needs to understand the differences in how interfaces get implemented between these two languages.

C# has two styles of implementation: implicit and explicit (see http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx). VB has only an explicit style, which doesn't work quite the same as C# (see https://msdn.microsoft.com/en-us/library/28e2e18x.aspx).

All of those "must implement" errors mean pretty much what they say: You must use the Implements keyword on the appropriate members of your subclass, because VB doesn't do implicit implementations of interfaces. That's a C# thing. When you hit the ENTER key with the cursor caret next to Implements IUnderlineableSpellingControl, the IDE generated template code for the affected (apparently missing) members, complete with Implements. It did that in trying to be helpful, but in this case you have to look over the code and put in the Implements clauses where they're needed (and probably get rid off that template code).

C# has a neat implicit style where it will automatically "wire-up" implementations by matching member names between your class and the interfaces being implemented. Should there be more than one interface that have the same member (with the same signature), they will all be implemented with the very same member in your class (see http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx). This can be a wonderful or not-so-good thing depending on the situation.

C# has a limited explicit style. One simply defines a member in the named in the format of InterfaceName.MemberName (see https://msdn.microsoft.com/en-us/library/ms173157.aspx).

VB has only its explicit style, but it allows for a list of interface members in the Implements clause so that they all get implemented by the very same member in your class. This is the work-around for C# implicit implementations that hookup multiple interfaces.

Finally, there are some sources which claim that VB cannot do re-implementations of an interface on a subclass of a superclass that already has it implemented (e.g. http://www.dotnetheaven.com/article/how-to-re-implement-interfaces-in-vb.net). I do not know if that was ever true, but I can aver that the VB of VS 2012 and later allows for such re-implementations.

这篇关于从C#到VB.NET的实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆