簡體   English   中英

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

[英]How Can I Modify The UInteger Type In Visual Basic .NET?

我要做的是創建一個名為Percent的結構。 它的范圍是從0到100的整數,並且只能用於普通類型,即:

Dim a as Integer

所以:

Dim b as Percent

我嘗試創建一個類,但我立刻明白Integer是一個結構,而不是一個類。 查看MSDN庫我找不到多少。


正如rcl發布的simon,想法是創建一個類似於Integer類型的結構,並通過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 

用法則為:Dim c as Percent Dim d as Percent c.Value = 99 d.Value = 26 If c.Value> d.Value Then End

我的解決方案中的騙局是你不能設置如下值:Dim e as Percent = 24相反,你需要訪問Value屬性並對其進行操作:Dim f as Percent f.Value = 23' (如上例所示)。

您可以使用擴展轉換重載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

這將是相當多的工作。 開始相對容易(為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。 您可能還應該實現這些(或者在您發現需要它們時做好准備)。

因此,找出所有操作的答案,編寫操作覆蓋代碼,添加和實現您需要的接口,就在那里!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM