VB.Net使类的行为类似于主要类型,例如字符串或整数

时间:2016-10-25 16:54:25

标签: vb.net oop vb.net-2010

我已经让类充当变体MathVariant,它将保存任何值,但总是使用数值(0或1)进行简单算术。我仍然需要使用乘法和除法进行更多测试,但除此之外它似乎运行良好。

我希望MathVariant表现得像一个原始类型,所以我可以像普通变量一样使用它。所以现在当我想获得值时,我必须得到.Value属性。

这是我希望能够编写代码的代码示例

Dim SomeValue As MathVariant = "12.11"
Dim AnotherValue As MathVariant = "3"
Dim Result As MathVariant = SomeValue + AnotherValue 
Console.WriteLine(Result) 'prints MyNameSpace.MathVariant
Console.WriteLine(Result.Value) 'returns desired result (15.11)

我希望能够使用该类的实例来直接获取其值。我一直在寻找很长一段时间,我不确定它是否可能或如何做到这一点。我不确定我是否必须摆脱Value Property或者是否有保留的名称或者是什么,我意识到我的类代码很长,所以如果有人可以解释必须做出的改变并且可能显示一个例子一个或两个可以改变的。

Public Class MathVariant
    Private m_Value As String

    'public version
    Public Property Value() As Object
        Get
            Dim m_value1 As Double
            If Double.TryParse(m_Value, m_value1) Then
                Return m_value1
            End If
            Return m_Value
        End Get
        Set(value As Object)
            m_Value = value.ToString()
        End Set
    End Property

    Public Sub New(Value As String)
        m_Value = Value
    End Sub

    Public Shared Widening Operator CType(ByVal value As String) As MathVariant
            Return New MathVariant(value)
    End Operator

    Public Shared Widening Operator CType(ByVal value As Decimal) As MathVariant
        Return New MathVariant(value.ToString())
    End Operator

    Public Shared Widening Operator CType(ByVal value As Double) As MathVariant
        Return New MathVariant(value)
    End Operator

    Public Shared Widening Operator CType(ByVal value As Integer) As MathVariant
        Return New MathVariant(value.ToString())
    End Operator

    Public Shared Narrowing Operator CType(ByVal value As MathVariant) As Double
        Return value.ToDouble()
    End Operator

    Public Shared Narrowing Operator CType(ByVal value As MathVariant) As Integer
        Return Convert.ToInt32(value.ToDouble())
    End Operator

    Public Shared Narrowing Operator CType(ByVal value As MathVariant) As String
        Return value.Value.ToString()
    End Operator

    Public Function ToDouble() As Double
        Dim test As Double
        If Double.TryParse(Me.Value.ToString(), test) Then
            Return test
        End If
        Return 0
    End Function

    'addition
    Public Shared Operator +(value1 As MathVariant, value2 As MathVariant) As MathVariant
        If TypeOf value1.Value Is Double And TypeOf value2.Value Is Double Then
            Return value1.ToDouble() + value2.ToDouble()
        ElseIf TypeOf value1.Value Is Double Then
            Return value1.ToDouble() + 0.0
        ElseIf TypeOf value2.Value Is Double Then
            Return 0.0 + value2.ToDouble()
        End If
        Return 0
    End Operator

    Public Shared Operator +(value1 As MathVariant, value2 As Object) As MathVariant
        If TypeOf value1.Value Is Double And TypeOf value2 Is Double Then
            Return value1.ToDouble() + value2
        ElseIf TypeOf value2 Is Double Then
            Return 0.0 + value2
        End If
        Return 0.0
    End Operator

    Public Shared Operator +(value1 As MathVariant, value2 As Double) As MathVariant
        If TypeOf value1.Value Is Double Then
            Return value1.ToDouble() + value2
        End If
        Return 0.0 + value2
    End Operator

    Public Shared Operator +(value1 As MathVariant, value2 As Integer) As MathVariant
        If TypeOf value1.Value Is Double Then
            Return value1.ToDouble() + Convert.ToDouble(value2)
        End If
        Return 0.0 + Convert.ToDouble(value2)
    End Operator

    'subtraction
    Public Shared Operator -(value1 As MathVariant, value2 As MathVariant) As MathVariant
        If TypeOf value1.Value Is Double And TypeOf value2.Value Is Double Then
            Return value1.ToDouble() - value2.ToDouble()
        ElseIf TypeOf value1.Value Is Double Then
            Return value1.ToDouble() - 0.0
        ElseIf TypeOf value2.Value Is Double Then
            Return 0.0 - value2.ToDouble()
        End If
        Return 0
    End Operator

    Public Shared Operator -(value1 As MathVariant, value2 As Object) As MathVariant
        If TypeOf value1.Value Is Double And TypeOf value2 Is Double Then
            Return value1.ToDouble() - value2
        ElseIf TypeOf value2 Is Double Then
            Return 0.0 - value2
        End If
        Return 0.0
    End Operator

    Public Shared Operator -(value1 As MathVariant, value2 As Double) As MathVariant
        If TypeOf value1.Value Is Double Then
            Return value1.ToDouble() - value2
        End If
        Return 0.0 - value2
    End Operator

    Public Shared Operator -(value1 As MathVariant, value2 As Integer) As MathVariant
        If TypeOf value1.Value Is Double Then
            Return value1.ToDouble() - Convert.ToDouble(value2)
        End If
        Return 0.0 - Convert.ToDouble(value2)
    End Operator

    'division
    Public Shared Operator /(value1 As MathVariant, value2 As MathVariant) As MathVariant
        If TypeOf value1.Value Is Double And TypeOf value2.Value Is Double Then
            Return value1.ToDouble() / value2.ToDouble()
        ElseIf TypeOf value1.Value Is Double Then
            Return value1.ToDouble() / 1.0
        ElseIf TypeOf value2.Value Is Double Then
            Return 1.0 / value2.ToDouble()
        End If
        Return 0
    End Operator

    Public Shared Operator /(value1 As MathVariant, value2 As Object) As MathVariant
        If TypeOf value1.Value Is Double And TypeOf value2 Is Double Then
            Return value1.ToDouble() / value2
        ElseIf TypeOf value2 Is Double Then
            Return 1.0 / value2
        End If
        Return 0.0
    End Operator

    Public Shared Operator /(value1 As MathVariant, value2 As Double) As MathVariant
        If TypeOf value1.Value Is Double Then
            Return value1.ToDouble() + value2
        End If
        Return 0.0 + value2
    End Operator

    Public Shared Operator /(value1 As MathVariant, value2 As Integer) As MathVariant
        If TypeOf value1.Value Is Double Then
            Return value1.ToDouble() + Convert.ToDouble(value2)
        End If
        Return 0.0 + Convert.ToDouble(value2)
    End Operator

    'multiplication
    Public Shared Operator *(value1 As MathVariant, value2 As MathVariant) As MathVariant
        If TypeOf value1.Value Is Double And TypeOf value2.Value Is Double Then
            Return value1.ToDouble() * value2.ToDouble()
        ElseIf TypeOf value1.Value Is Double Then
            Return value1.ToDouble() * 1.0
        ElseIf TypeOf value2.Value Is Double Then
            Return 1.0 * value2.ToDouble()
        End If
        Return 1.0
    End Operator

    Public Shared Operator *(value1 As MathVariant, value2 As Object) As MathVariant
        If TypeOf value1.Value Is Double And TypeOf value2 Is Double Then
            Return value1.ToDouble() * value2
        ElseIf TypeOf value2 Is Double Then
            Return 1.0 * value2
        End If
        Return 1.0
    End Operator

    Public Shared Operator *(value1 As MathVariant, value2 As Double) As MathVariant
        If TypeOf value1.Value Is Double Then
            Return value1.ToDouble() + value2
        End If
        Return 1.0 * value2
    End Operator

    Public Shared Operator *(value1 As MathVariant, value2 As Integer) As MathVariant
        If TypeOf value1.Value Is Double Then
            Return value1.ToDouble() * Convert.ToDouble(value2)
        End If
        Return 1.0 * Convert.ToDouble(value2)
    End Operator


    'less than
    Public Shared Operator <(value1 As MathVariant, value2 As MathVariant)
        If TypeOf value1.Value Is Double And TypeOf value2.Value Is Double Then
            Return value1.Value < value2.Value

        ElseIf TypeOf value1.Value Is Double Then
            Return value1.Value < 0

        ElseIf TypeOf value2.Value Is Double Then
            Return 0 < value2.Value
        End If
        Return False
    End Operator

    Public Shared Operator <(value1 As MathVariant, value2 As Double)
        If TypeOf value1.Value Is Double Then
            Return value1.Value < value2
        Else
            Return 0 < value2
        End If
        Return False
    End Operator

    Public Shared Operator <(value1 As MathVariant, value2 As Integer)
        If TypeOf value1.Value Is Double Then
            Return value1.Value < Convert.ToDouble(value2)
        Else
            Return 0.0 < Convert.ToDouble(value2)
        End If
        Return False
    End Operator

    'greater than
    Public Shared Operator >(value1 As MathVariant, value2 As MathVariant)
        If TypeOf value1.Value Is Double And TypeOf value2.Value Is Double Then
            Return value1.Value > value2.Value

        ElseIf TypeOf value1.Value Is Double Then
            Return value1.Value > 0

        ElseIf TypeOf value2.Value Is Double Then
            Return 0 > value2.Value
        End If
        Return False
    End Operator

    Public Shared Operator >(value1 As MathVariant, value2 As Double)
        If TypeOf value1.Value Is Double Then
            Return value1.Value > value2
        Else
            Return 0 > value2
        End If
        Return False
    End Operator

    Public Shared Operator >(value1 As MathVariant, value2 As Integer)
        If TypeOf value1.Value Is Double Then
            Return value1.Value > Convert.ToDouble(value2)
        Else
            Return 0.0 > Convert.ToDouble(value2)
        End If
        Return False
    End Operator

    'equal to
    Public Shared Operator =(value1 As MathVariant, value2 As Object)
        If TypeOf (value2) Is Double Then
            Return Convert.ToDouble(value2).Equals(value1.ToDouble())
        ElseIf TypeOf (value2) Is String Then
            Return Convert.ToString(value2).Equals(value1.ToString())
        End If
        Return False
    End Operator

    'not equal
    Public Shared Operator <>(value1 As MathVariant, value2 As Object)
        Dim m_Type As Type = value2.GetType()
        If TypeOf (value2) Is Double Then
            Return Convert.ToDouble(value2).Equals(value1.ToDouble())
        ElseIf TypeOf (value2) Is String Then
            Return Convert.ToString(value2).Equals(value1.ToString())
        End If
        Return False
    End Operator
End Class

修改

以下是我使用上述代码制作的.NetFiddle的链接。

https://dotnetfiddle.net/bFlyhG

1 个答案:

答案 0 :(得分:1)

除了使用Console.WriteLine()时,它似乎适用于大多数情况。覆盖ToString() method似乎可以解决问题。

Public Overrides Function ToString() As String
    Return m_Value
End Function

也请安德鲁建议开启Option Strict(至少暂时)。通过这样做,您可以看到代码中需要修复的内容,以使其尽可能最佳(转换方式)。