Option Explicit On
Option Strict On

'###########################
'Class: VBridge
'Author: Adam "wossname" Ward
'Date: 04 April 2005
'Purpose: Some old VB6 functions ported to real framework compatible code
'###########################

Imports System.Text

Public NotInheritable Class VBridge

#Region "Member Data"
	Private Shared _buffer() As Char = Nothing
	Private Shared _enc As ASCIIEncoding = Nothing
	Private Shared _Random As Random = Nothing
#End Region

#Region "Public"
	Public Shared Function IsNothing(ByVal obj As Object) As Boolean
		'if you need this then you really do suck at writing code :D
		Return obj Is Nothing
	End Function

	Public Shared Function IsSomething(ByVal obj As Object) As Boolean
		'I sometimes use IsSomething to improve readability
		Return Not (obj Is Nothing)
	End Function

	Public Shared Function IPart(ByVal value As Double) As Integer
		'IntegerPart
		'equivalent of vb6's Fix() function.  (Behaves like Int() when Value is positive)
		'eg. for input of "132.435" this returns 132
		'eg. for input of "-132.435" this returns -132
		Return CType(value - FPart(value), Integer)
	End Function

	Public Shared Function FPart(ByVal value As Double) As Double
		'FractionalPart, returns the decimal portion of a number
		'eg. for input of "132.435" this returns 0.435
		'eg. for input of "-132.435" this returns -0.435
		Return value Mod 1
	End Function

	Public Shared Function Chr(ByVal ascii As Byte) As Char
		'returns the char for a given ascii code

		If _buffer Is Nothing Then InitCharBuffer()
		Return _buffer(ascii)

	End Function

	Public Shared Function Chr(ByVal ascii As Short) As Char
		'returns the char for a given ascii code

		If ascii < 0 Or ascii > Byte.MaxValue Then Throw New IndexOutOfRangeException

		If _buffer Is Nothing Then InitCharBuffer()
		Return _buffer(ascii)

	End Function

	Public Shared Function Chr(ByVal ascii As Integer) As Char
		'returns the char for a given ascii code

		If ascii < 0 Or ascii > Byte.MaxValue Then Throw New IndexOutOfRangeException

		If _buffer Is Nothing Then InitCharBuffer()
		Return _buffer(ascii)

	End Function

	Public Shared Function Asc(ByVal character As Char) As Byte
		'returns the ascii code for a given char

		If _buffer Is Nothing Then InitCharBuffer()
		Return _enc.GetBytes(character)(0)

	End Function

	Public Shared Function Rnd() As Double
		'returns a random number between (0 <= Rnd < 1)

		If _Random Is Nothing Then InitRandom()
		Return _Random.NextDouble

	End Function

	Public Shared Function LeftEx(ByVal str As String, ByVal numChars As Integer) As String
		'returns the Leftmost characters of str

		If str Is Nothing Then Throw New ArgumentNullException

		If numChars > str.Length Then Throw New ArgumentException("numChars argument is greater than the length of the string.")
		If numChars = 0 Then
			Return ""
		Else
			Return str.Substring(0, numChars)
		End If
	End Function

	Public Shared Function RightEx(ByVal str As String, ByVal numChars As Integer) As String
		'returns the Rightmost characters of str

		If str Is Nothing Then Throw New ArgumentNullException

		If numChars > str.Length Then Throw New ArgumentException("numChars argument is greater than the length of the string.")
		If numChars = 0 Then
			Return ""
		Else
			Return str.Substring(str.Length - numChars, numChars)
		End If
	End Function

	Public Shared Function Val(ByVal value As String) As Double
		'if the string is a valid number this function returns a double holding that value
		'defaults to NaN

		Dim temp As Double
		Dim ifp As IFormatProvider

		If Double.TryParse(value, Globalization.NumberStyles.Any, ifp, temp) Then
			Return temp
		Else
			Return Double.NaN
		End If
	End Function

	Public Shared Function Val0(ByVal value As String) As Double
		'if the string is a valid number this function returns a double holding that value
		'defaults to zero

		Dim temp As Double
		Dim ifp As IFormatProvider

		If Double.TryParse(value, Globalization.NumberStyles.Any, ifp, temp) Then
			Return temp
		Else
			Return 0
		End If
	End Function

#End Region

#Region "Private"
	Private Shared Sub InitCharBuffer()

		'fills the _buffer with ascending char values, for example _buffer(65) contains "A"
		Dim temp As Byte() = New Byte(255) {}

		For i As Integer = 0 To 255
			temp(i) = CType(i, Byte)
		Next i

		_enc = New ASCIIEncoding
		_buffer = _enc.GetChars(temp)

	End Sub

	Private Shared Sub InitRandom()

		_Random = New Random(Environment.TickCount)

	End Sub

#End Region

End Class

