创建类似sbyte,uint16等的个体积分数据类型。 [英] Create an individual integral data type the like of sbyte, uint16 etc..

查看:123
本文介绍了创建类似sbyte,uint16等的个体积分数据类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个定义数值的结构,范围从1到114,与Int32等相同。

我知道这种方法很重但可能有一些方法可以让IDE知道有不仅是Int32或Uint32而且,XUint,(笑)

我如何在VB.net或C#net中实现这一点,谢谢



I want to create a structure that defines numerical values ranging from 1to114 same as Int32 etc. work.
I know this approach is heavy but maybe there some way to make the IDE know that there is not only Int32 or Uint32 but also, "XUint", (lol)
How do i achieve this in VB.net or C#net, thanks

Public Structure XUInt
  public shared const MinValue = 1
  public shared const MaxValue = 114
  ...?

...implement casting type CXUInt(i)?

End Structure

Module GlobalRef
 public num1 As XUint = 5 'direct cast
 public num2 As XUint = CXUint(5/2) 'manual casting reference?
End Module





我们的想法是让用户知道在使用声明为XUInt的整数属性时必须进行强制转换。



我尝试过:



__________________________



The idea is to let the user know that one has to cast when using a integral Property declared as XUInt.

What I have tried:

__________________________

推荐答案

感谢您的澄清。



你可以定义这样的类,只有最好通过构造函数传递最小和最大参数,才能使它至少有点普遍。然后,您可以为不同的整数类型(或仅一个)定义转换运算符,隐式显式。在这些运算符的实现中,您可以定义有效范围的检查。请参阅:

转换运算符(C#编程指南)

转换和类型转换(C#编程指南)
显式(C#参考)

< a href =https://msdn.microsoft.com/en-us/library/z5z9kes2.aspx>隐式(C#参考)。



如果需要,可以添加一些其他操作符,例如算术,使用相同类型的值检查。



-SA
Thank you for your clarifications.

You can define such class, only it's better to make minimum and maximum parameters passed through a constructor, to make it at least a bit universal. Then you can define, say, conversion operators for different integer types (or just one), implicit or explicit. In the implementation of these operator, you can define check up for the valid range. Please see:
Conversion Operators (C# Programming Guide),
Casting and Type Conversions (C# Programming Guide),
explicit (C# Reference),
implicit (C# Reference).

You can add some other operators if you need them, arithmetic for example, with the same kind of the value check.

—SA


查看模数:%运算符。

X%n将始终给出0到n -1之间的结果
Look at modulus: the % operator.
X % n will will always give a result between 0 and n -1


这篇关于创建类似sbyte,uint16等的个体积分数据类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆