Custom data type

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
Hey guys!

I do not have the correct words to look for a solution. So I'll try to explain what I want. Is it possible to achieve something like this:

Visual Basic:
Dim angle1 as Angle = Math.Pi*3 '=Math.Pi
Dim angle2 as Angle = Math.Pi*3/2
Dim angle3 as Angle = angle1 + angle2
debug.print(angle3) 'Would print 1.57 which is Math.Pi/2

Angle would be a custom class or type or whatever is the right word/solution and inside that "class" definition would be defined that these types can be added(+) and Angle is allways between [0...Math.Pi*2[.
 
You would want to look at operator overloading for this kind of thing.

Something like the following might get you started - the general idea is there it would just need validation adding and the correct maths implemented :D
Visual Basic:
Public Structure Angle

    Private _Angle As Decimal

    Public Shared Operator +(ByVal angle1 As Angle, ByVal angle2 As Angle) As Angle

        Dim a As Angle
        a._Angle = angle1._Angle + angle2._Angle

        Return a
    End Operator

    Public Shared Widening Operator CType(ByVal d As Decimal) As Angle
        Dim a As Angle

        'validate the angle here
        a._Angle = d

        Return a
    End Operator

    Public Shared Widening Operator CType(ByVal a As Angle) As Decimal
        Return a._Angle
    End Operator

End Structure
 
Back
Top