我可以在 vb.net 结构中定义赋值运算符吗?
我的结构实际上是一个具有更多功能的简单字节。
我是这样定义的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个转换运算符,例如
UPDATE:
对于您建议的
Int4
类型,我强烈建议您将其改为Narrowing
运算符。这会强制代码的用户显式转换,这是一个视觉提示,表明分配可能在运行时失败,例如Create a conversion operator, e.g.
UPDATE:
For your proposed
Int4
type I strongly suggest that you make it aNarrowing
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.