I'm trying to make a Class library with a function to convert binary integers to denary, and vice versa, so that I can import it into another project without having to rewrite the function. It works fine, here's part of the class:
Public Class BinaryDenary
Public Shared Function ToBinary(ByVal DenaryNumber As Integer) As Integer
Dim Binary As String = ""
While DenaryNumber > 0
If DenaryNumber Mod 2 = 1 Then
Binary = 1 & Binary
Else
Binary = 0 & Binary
End If
DenaryNumber \= 2
End While
Return CInt(Binary)
End Function
End Class
I've tested it within the project and it works fine.
ToBinary(3) 'Returns 11
ToDenary(110) 'Returns 6
But - mostly for aesthetic reasons - I'd like to be able to use it like an extension method, so that I can take a variable and do this:
NormalInt.ToBinary(3)
But I can't write extension methods inside of a class. Is there any way of doing this? It's not hugely important, but I like to use extension methods where I can.