Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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[.

  • Administrators
Posted

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

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...