简体   繁体   English

继承VB中的接口-从C#移植代码

[英]Inherits Interface in VB - Porting code from C#

So, I'm working with a library (Coderr, for anyone familiar with it), that's written in C#, and so are their examples. 因此,我正在使用用C#编写的库(Coderr,熟悉它的人),它们的示例也是如此。 My project is of course in VB, and I'm a bit confused on implementing a template of their example classes. 我的项目当然是在VB中进行的,我对实现其示例类的模板有些困惑。

An example of one of their existing classes is here . 这里是其中一个现有类的示例。

I've attempted to port it by hand, and failed, so used Telerik's C# to VB converter. 我试图手动移植它,但失败了,所以使用了Telerik的C#到VB转换器。 It outputs code that looks pretty good, and with only one tweak, the class itself works great. 它输出看起来很不错的代码,并且只需进行一次调整,该类本身就可以很好地工作。 Except the inherits clause. 除了继承子句。

The code I've got it to now: 我现在掌握的代码:

Namespace codeRR.Client.AspNet.CurrentUserContext    
    Public Class CurrentUserContext
        Inherits IContextInfoProvider

        Public Function Collect(ByVal context As IErrorReporterContext) As ContextCollectionDTO
            Dim CurrentUser As CurrentUser = CType(HttpContext.Current.Session("CurrentUser"), CurrentUser)
            If CurrentUser Is Nothing Then Return Nothing
            Dim converter = New ObjectToContextCollectionConverter()
            Dim collection = converter.Convert(Name, CurrentUser)
            Return If(collection.Properties.Count = 0, Nothing, collection)
        End Function

        Public ReadOnly Property Name As String
            Get
                Return "CurrentUser"
            End Get
        End Property
    End Class
End Namespace

So very similar to the example code, but returning my custom object instead, basically. 因此与示例代码非常相似,但是基本上返回了我的自定义对象。 The Inherits line fails with: 继承行失败并显示:

Classes can only inherit from other classes. 类只能从其他类继承。

Which does make sense, since IContextInfoProvider is an Interface. 确实有意义,因为IContextInfoProvider是一个接口。 I'm just stuck on how I actually use it in VB. 我只是停留在我如何在VB中实际使用它。 I need to plug the class into a function that accepts an object of type IContextInfoProvider . 我需要将类插入接受IContextInfoProvider类型的对象的函数中。

The error gives you a good indication of what the problem is. 该错误可以很好地指示问题所在。 While a Class can inherit from another Class it can't inherit from an Interface. 虽然一个类可以从另一个类继承,但不能从接口继承。

Inheritance implies deriving some actual implementation of behaviour which a Class contains, as well as its 'signature' whereas an Interface simply defines its 'signature' with an expectation of the implementation of suitable behaviour in the implementing Class, hence it can only be implemented. 继承意味着派生某个类所包含的行为的某些实际实现及其“签名”,而接口只是在期望实现类中实现适当行为的情况下简单地定义其“签名”,因此只能实现。

So, 所以,

Public Class CurrentUserContext
    Inherits IContextInfoProvider

should be 应该

Public Class CurrentUserContext
    Implements IContextInfoProvider

Classes can inherit from other classes or they can implement interfaces. 类可以从其他类继承,也可以实现接口。 Interfaces traditionally start with an upper-case I , such as IContextInfoProvider . 传统上,接口以大写I开头,例如IContextInfoProvider

In C#, a class can implement an interface or inherit from another class using the same syntax: 在C#中,一个类可以使用相同的语法实现接口或从另一个类继承:

// Implement an interface
public class CurrentUserContext : IContextInfoProvider

// Inherit from a class
public class CurrentUserContext : MyContextBaseClass

In VB.Net, however, a class that implements an interface or a class that inherits from another class takes on two different syntax's: 但是,在VB.Net中,实现接口的类或从另一个类继承的类采用两种不同的语法:

' Implement an interface
Public Class CurrentUserContext
    Implements IContextInfoProvider

' Inherit from a class
Public Class CurrentUserContext
    Inherits MyContextBaseClass

In your case, you should be using Implements instead of Inherits since you are implementing an interface and not inheriting from a class. 在您的情况下,您应该使用Implements而不是Inherits因为您正在实现接口而不是从类继承。

I hope this clears things up! 我希望这可以清除一切!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM