开发者

Restricting an interface to structures only in vb.net

I have an interface IDigitalState defined as

Public Interface IDigitalState

    ReadOnly Property Code As Integer
    ReadOnly Property Name As String

End Interface

and a structure that implements this interface

Public Structure DigitalState
    Implements IDigitalState

    Private ReadOnly mCode As Integer
    Private ReadOnly mName As String

    Public ReadOnly Property Code As Integer Implements IDigitalState.Code
        Get
            Return mCode
        End Get
    End Property

    Public ReadOnly Property Name As String Implements IDigitalState.Name
        Get
            Return mName
        End Get
    End Property

    Public Sub New(ByVal code As Integer, name As String)

        mCode = code
        mName = name

 开发者_Python百科   End Sub

End Structure

What I wanted to do was declare a variable as a nullable type of IDigitalState. I understand why I cant do this because the interface may be implemented by a class which is not allowed to be nullable. Is there a way to define the interface so that it can only be implemented by a structure. I'm doubting it's possible but thought it would be worth looking into.


You can do this in combination with generics. For instance:

Sub Test(Of T As {IDigitalState, Structure})()
    Dim something As T? = GetEitherValueOrNull …
End Sub

The key here is that you operate on a concrete (generic) type T which has two conditions:

  1. it is a structure, and
  2. it implements IDigitalState.

Or you can just use a normal variable of interface type, which can be Nothing, without the need for a Nullable.


No, there is no way.

However, you can type.

Dim nullableIDigitalState As IDigitalState = nothing

which would be declaring a variable of type IDigitalState as null. If you are talking about the Nullable<> generic that has a where constraint that limits to value types so it would only accept a structure variant of IDigitalState.

Am I missing your point?


The only situations in which it would be meaningful to restrict interface implementations to structure types are those in which the interface type is going to be used as a generic constraint, and never as a storage location type. In such situations, any code which requires type parameter to be a struct that implements the interface can specify that. Nothing would prevent classes from implementing the interface, but so what? A variable of type IDigitalState could hold a reference to a class that implements that interface, but could not be passed as a generic parameter of type T As {Structure,IDigitalState}, so code which requires a structure-type implementation wouldn't care that such things might exist.

Note, btw, that storing a struct that implements IDigitalState into a variable of type IDigitalState will effectively create a new class object with fields matching those of the structure, and store a reference to that object. If you wish to ensure that a struct which implements an interface, will behave like a struct rather than a class, you need to pass or hold it in a variable with an interface-constrained generic type, rather than in a variable of the interface type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜