我可以在 vb.net 结构中定义赋值运算符吗?

发布于 2024-09-08 23:59:41 字数 751 浏览 6 评论 0原文

我的结构实际上是一个具有更多功能的简单字节。

我是这样定义的:

Structure DeltaTime

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

End Structure

我想拥有这两个功能:

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

I am having a structure that is actually a simple byte with more functionality.

I defined it this way:

Structure DeltaTime

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

End Structure

I want to have these two functionalities:

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

Is there a way to achieve this?

If there would be a way to inherit from Structure I would simply inherit from Byte adding my functionality, basically I just need a byte Structure with custom functionality.

My question is also valid when you want to define your new singleton member value-types (like, you want to define a nibble-type for instance etc.) and you want to be able to set it with an assignment to number or other language typed representation.

In other words, I want to be able to do define the following Int4 (nibble) stucture and use it as follows:

Dim myNibble As Int4 = &HF 'Unsigned

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2024-09-15 23:59:41

创建一个转换运算符,例如

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

UPDATE:

对于您建议的 Int4 类型,我强烈建议您将其改为 Narrowing 运算符。这会强制代码的用户显式转换,这是一个视觉提示,表明分配可能在运行时失败,例如

Dim x As Int4 = CType(&HF, Int4) ' should succeed
Dim y As Int4 = CType(&HFF, Int4) ' should fail with an OverflowException

Create a conversion operator, e.g.

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

UPDATE:

For your proposed Int4 type I strongly suggest that you make it a Narrowing operator instead. This forces the user of your code to cast explicitly, which is a visual hint that the assignment might fail at runtime, e.g.

Dim x As Int4 = CType(&HF, Int4) ' should succeed
Dim y As Int4 = CType(&HFF, Int4) ' should fail with an OverflowException
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文