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

时间:2015-04-03 14:49:30

标签: .net vb.net visual-studio

我要做的是制作一个名为Percent的结构。它的范围是整数从0到100 ,并且只能用于普通类型,即:

Dim a as Integer

所以:

Dim b as Percent

我尝试创建一个类,但我立刻明白Integer是一个结构,而不是一个类。查看MSDN库我找不到多少。

解决方案

<小时/> 正如rcl发布的simon,我们的想法是创建一个类似于Integer类型的结构,并通过Value-&gt; 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

用法将是:     Dim c as Percent     昏暗的百分比     c.Value = 99     d.Value = 26     如果c.Value&gt; d.Value Then End

我的解决方案中的骗局是你无法设置如下值:     Dim e as Percent = 24 相反,您需要访问 Value 属性并对其进行操作:     昏暗的百分比     f.Value = 23'(如上例所示)。

2 个答案:

答案 0 :(得分:2)

您可以使用扩展转换重载CType运算符以进行隐式转换。您必须为要以此方式分配的每种类型重载它。

然后你可以使用这样的代码:

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

答案 1 :(得分:1)

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

public struct Percent
{
    private uint pcnt;

    public Percent(int value)
    {
        this.pcnt = (uint)value;
        if (this.pcnt > 100)
        {
            throw new ArgumentOutOfRangeException("Value > 100");
        }
    }
    public uint Value
    {
        get { return this.pcnt; }
        set
        {
            if (value > 100)
            {
                throw new ArgumentOutOfRangeException("Value > 100");
            }
            this.pcnt = value;
        }
    }
}

可以使用如下:

    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;

你不能做Percent p= 57;的原因是因为虽然可以在VB或C#中为int等编写,但编译器会特殊情况并在后台生成额外的代码。

现在努力工作开始了。你想输入Percent p2 = p * p2 - 但我的问题是这是什么意思?我们在上面的代码之后得到的两个值是57和47;我认为将任何值乘以例如百分比57%实际上乘以57 / 100.因此,你必须通过乘法来决定你的意思,然后重写乘法运算并对其进行编码。这也需要覆盖您可能想要使用的任何数字类型:int32,int64,decimal,uint32,uint64,您可能使用的任何其他类型。完成所有这些操作后,您可以编写decimal adjusted = 123.45 * p;

之类的内容

然后,对于您可能想要使用的任何其他操作重复此操作,例如添加。我可以看到在一起添加两个百分比可能是有用的(如果它们总和为> 100会发生什么?),但是为int或其他数字添加百分比?那是做什么的?

相反,将一个百分比除以另一个百分比对我来说并没有多大意义,但将百分比除以一个int(将int除以百分比)。

然后有需要实现的接口。 UInt实现了IComparable,IFormattable,IConvertible,IComparable和IEquatable。你可能也应该实现这些(或者当你发现需要它们时做好准备)。

因此,请为所有操作找出答案,编写操作覆盖代码,添加并实现您需要的界面,以及您所拥有的界面!