Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Visual Basic.NET中修改UInteger类型?_.net_Vb.net_Visual Studio - Fatal编程技术网

如何在Visual Basic.NET中修改UInteger类型?

如何在Visual Basic.NET中修改UInteger类型?,.net,vb.net,visual-studio,.net,Vb.net,Visual Studio,我想做的是制作一个名为%的结构。它的范围将是一个从0到100的整数,并且可以仅用于普通类型,即: Dim a as Integer 因此: 我试图创建一个类,但我立即明白Integer是一个结构而不是一个类。在MSDN库中我找不到太多 解决方案 正如rcl的simon所说,其想法是制作一个类似于整数类型的结构,并通过Value->Set部分限制它可以接受的值 Public Structure Percent Dim PValue As Integer Public Prop

我想做的是制作一个名为%的结构。它的范围将是一个从0到100的整数,并且可以仅用于普通类型,即:

Dim a as Integer
因此:

我试图创建一个类,但我立即明白Integer是一个结构而不是一个类。在MSDN库中我找不到太多

解决方案
正如rcl的simon所说,其想法是制作一个类似于整数类型的结构,并通过Value->Set部分限制它可以接受的值

Public Structure Percent
    Dim PValue As Integer

    Public Property Value As UInteger
        Get
            Return PValue
        End Get
        Set(ByVal value As UInteger)
            'Make sure the value is valid...
            If value > 100 Then
                Throw New PercentTooBigException("You cannot give a percentage value greater than a 100.")
            ElseIf value < 0 Then
                Throw New PercentTooSmallException("You cannot give a percentage value smaller than 0.")
            End If
            '... and if it is set the local PValue to the value value.
            PValue = value
        End Set
    End Property
End Structure
公共结构百分比
将PValue设置为整数
公共财产作为单位价值
得到
返回PValue
结束
设置(ByVal值作为UInteger)
'确保该值有效。。。
如果值>100,则
抛出新的PercentTooBigException(“您不能给出大于100的百分比值。”)
ElseIf值<0则
抛出新的PercentTooSmallException(“您不能给出小于0的百分比值。”)
如果结束
'... 如果它被设置为本地PValue,则为该值。
PValue=值
端集
端属性
端部结构
使用方法如下: 以百分比表示的尺寸c d表示为百分比 c、 值=99 d、 值=26 如果c.值>d.值,则结束

我的这个解决方案的缺点是不能设置如下值: 尺寸e的百分比=24 相反,您需要访问Value属性并对其进行操作: 以百分比表示的尺寸f
f、 Value=23'(如上面的示例所示)。

这将是一项相当大的工作。开始相对容易(为C#道歉):

这可以按如下方式使用:

    Percent p  = new Percent(57);
    Console.WriteLine("Value = {0}", p.Value);
    Percent p2 = new Percent(44);
    Console.WriteLine("Value = 0", p2.Value);
    p2.Value = p.Value - 10;
无法执行
的原因百分比p=57是因为,尽管可以在VB或C#中为int等编写代码,但编译器会对其进行特殊处理,并在后台生成额外的代码

现在艰苦的工作开始了。您想键入
百分比p2=p*p2
——但我的问题是这意味着什么?我们在上面的代码后面的两个值是57和47;我认为将任何值乘以某个百分比,例如57%,实际上等于乘以57/100。因此,您必须确定乘法的含义,然后重写乘法运算并对其进行编码。这还需要覆盖您可能要使用的任何数字类型:int32、int64、decimal、uint32、uint64以及您可能使用的任何其他类型。一旦你完成了所有这些,你就可以写一些东西,比如
decimaladjusted=123.45*p

然后,对可能要使用的任何其他操作重复此操作,例如加法。我可以看出,将两个百分比相加可能会很有用(如果它们的总和大于100会发生什么情况?),但是将一个百分比添加到int或其他数字中?那有什么用

相反,用一个百分比除以另一个百分比对我来说没有多大意义,但是用一个百分比除以一个整数(就像用一个百分比除以一个整数一样)

然后是需要实现的接口。UInt实现了IComparable、IFormattable、IConvertible、IComparable和IEquatable。您可能还应该实现这些(或者在发现需要它们时做好准备)


因此,为所有操作找出答案,编写操作覆盖代码,添加并实现所需的接口,就在这里

对于隐式转换,可以使用加宽转换重载CType运算符。对于要以这种方式分配的每个类型,都必须重载它

然后您可以使用如下代码:

Dim pcnt As Percent = 42
Console.WriteLine("The percent is {0}%", pcnt)
代码:

公共结构百分比
作为UInteger的Dim PValue
Public Sub New(值为整数)
Me.PValue=CType(值,UInteger)
端接头
公共财产作为单位价值
得到
返回PValue
结束
设置(ByVal值作为UInteger)
'使用无效值执行某些操作
如果值>100,则
p值=100
其他的
如果值<0,则
PValue=0
其他的
PValue=值
如果结束
如果结束
端集
端属性
Public将函数ToString()重写为字符串
返回PValue.ToString()
端函数
公共共享加宽运算符CType(ByVal p作为整数)作为百分比
返回新百分比(p)
终端操作员
端部结构

我读了这篇文章,但我不明白如何将UInt类型限制为0到100之间的值。我现在考虑将此“结构”创建为一个类,并在构造New()时检查其赋值并使用运算符(如“=”)。有什么有用的主意吗?想要的“效果”是:在idea级别将a作为[New?]Percent=57进行调整,如果高于100则抛出异常,如果过低则也抛出异常。@Phantom您可以使用
将p作为新百分比调整为{.Value=12}
。另外,给它一个.ToString方法可能会很方便。哦,是的-您应该使用,它会为您指出,
PValue作为整数
(应该是
Private
),但是
公共属性值作为UInteger
。请注意,谢谢。我会在可能的时候解决的。太好了,谢谢。我也将发布Visual Basic代码。再次感谢,你说得对。你认为我需要在这个例子中使用覆盖吗,或者这是一个概念证明/例子?这甚至不能提升到概念证明。我甚至没有认真研究过如何实现这些操作,因为我不知道VB实现会有多大的不同。我认为,发布你最终得到的任何代码都是一件非常好的事情!如果有,请告诉我。干杯-在您发布代码时向上投票。一条注释:如果值<0,则不需要(严格地)错误,因为尝试将-ve编号分配给uint会引发错误。然而
    Percent p  = new Percent(57);
    Console.WriteLine("Value = {0}", p.Value);
    Percent p2 = new Percent(44);
    Console.WriteLine("Value = 0", p2.Value);
    p2.Value = p.Value - 10;
Dim pcnt As Percent = 42
Console.WriteLine("The percent is {0}%", pcnt)
Public Structure Percent
    Dim PValue As UInteger

    Public Sub New(value As Integer)
        Me.PValue = CType(value, UInteger)
    End Sub

    Public Property Value As UInteger
        Get
            Return PValue
        End Get
        Set(ByVal value As UInteger)
            'Do something with invalid values
            If value > 100 Then
                PValue = 100
            Else
                If value < 0 Then
                    PValue = 0
                Else
                    PValue = value
                End If
            End If
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return PValue.ToString()
    End Function

    Public Shared Widening Operator CType(ByVal p As Integer) As Percent
        Return New Percent(p)
    End Operator
End Structure