0

私は、実際にはより多くの機能を備えた単純なバイトである構造を持っています。

私はそれを次のように定義しました:

Structure DeltaTime

    Private m_DeltaTime As Byte
    Public ReadOnly DeltaTime As Byte
        Get
            Return m_DeltaTime
        End Get
    End Property

End Structure

次の 2 つの機能が必要です。

Public Sub Main
    Dim x As DeltaTime = 80 'Create a new instance of DeltaTime set to 80
    Dim y As New ClassWithDtProperty With { .DeltaTime = 80 }
End Sub

これを達成する方法はありますか?

Structure から継承する方法があれば、単純に Byte から継承して機能を追加します。基本的には、カスタム機能を備えた byte Structure が必要です。

私の質問は、新しいシングルトン メンバーの値の型を定義したい場合 (たとえば、ニブル型を定義したい場合など) にも有効であり、数値または他の言語への割り当てを使用して設定できるようにしたい場合にも有効です。型付き表現。

つまり、次の Int4 (ニブル) 構造を定義して、次のように使用できるようにしたいと考えています。

Dim myNibble As Int4 = &HF 'Unsigned
4

1 に答える 1

2

変換演算子を作成します。

Structure DeltaTime

    Private m_DeltaTime As Byte
    Public ReadOnly Property DeltaTime() As Byte
        Get
            Return m_DeltaTime
        End Get
    End Property

    Public Shared Widening Operator CType(ByVal value As Byte) As DeltaTime
        Return New DeltaTime With {.m_DeltaTime = value}
    End Operator

End Structure

アップデート:

提案されたタイプについては、代わりに演算子にInt4することを強くお勧めします。Narrowingこれにより、コードのユーザーは明示的にキャストする必要があります。これは、割り当てが実行時に失敗する可能性があることを示す視覚的なヒントです。

Dim x As Int4 = CType(&HF, Int4) ' should succeed
Dim y As Int4 = CType(&HFF, Int4) ' should fail with an OverflowException
于 2010-07-09T13:53:54.383 に答える