Option Explicit On 
Option Strict On

Imports System.Text

''' --------------------------------------------------------------------------------
''' Project:	RoundShapeLib
''' Class:	CharInitializer
''' <summary>
''' Easy way to provide Char variable initialisation to your programs
''' This class is most efficient if your programs uses many char operations in the ASCII encoding
''' </summary>
''' <remarks>
''' Author: Adam Ward
''' Date: 15 March 2005
''' </remarks>
''' --------------------------------------------------------------------------------
Public Class CharInitializer

#Region "Member Data"
	Private Shared _buffer() As Char = Nothing
	Private Shared _enc As ASCIIEncoding = Nothing
#End Region

#Region "Public"
	''' --------------------------------------------------------------------------------
	''' Class.Method:	CharInitializer.Chr
	''' <summary>
	''' Used mainly to initialise (or assign values to) char variables.
	''' This is a framework compliant version of the old VB6 Chr$() function
	''' </summary>
	''' <param name="index">
	''' 	The ASCII character code of the required char. 
	''' 	Value Type: <see cref="Byte" />	(System.Byte)
	''' </param>
	''' <returns><see cref="Char" />	(System.Char)</returns>
	''' <remarks>
	''' </remarks>
	''' --------------------------------------------------------------------------------
	Public Shared Function Chr(ByVal index As Byte) As Char

		If _buffer Is Nothing Then InitBuffer()
		Return _buffer(index)

	End Function

	''' --------------------------------------------------------------------------------
	''' Class.Method:	CharInitializer.Chr
	''' <summary>
	''' Used mainly to initialise (or assign values to) char variables.
	''' This is a framework compliant version of the old VB6 Chr$() function
	''' </summary>
	''' <param name="index">
	''' 	The ASCII character code of the required char. 
	''' 	Value Type: <see cref="Int16" />	(System.Int16)
	''' </param>
	''' <exception cref="System.IndexOutOfRangeException">
	''' 	Thrown when index is not in the range 0 to 255. 
	''' </exception>
	''' <returns><see cref="Char" />	(System.Char)</returns>
	''' <remarks>
	''' </remarks>
	''' --------------------------------------------------------------------------------
	Public Shared Function Chr(ByVal index As Short) As Char

		If index < 0 Or index > Byte.MaxValue Then Throw New IndexOutOfRangeException

		If _buffer Is Nothing Then InitBuffer()
		Return _buffer(index)

	End Function

	''' --------------------------------------------------------------------------------
	''' Class.Method:	CharInitializer.Chr
	''' <summary>
	''' Used mainly to initialise (or assign values to) char variables.
	''' This is a framework compliant version of the old VB6 Chr$() function
	''' </summary>
	''' <param name="index">
	''' 	The ASCII character code of the required char. 
	''' 	Value Type: <see cref="Int32" />	(System.Int32)
	''' </param>
	''' <exception cref="System.IndexOutOfRangeException">
	''' 	Thrown when index is not in the range 0 to 255. 
	''' </exception>
	''' <returns><see cref="Char" />	(System.Char)</returns>
	''' <remarks>
	''' </remarks>
	''' --------------------------------------------------------------------------------
	Public Shared Function Chr(ByVal index As Integer) As Char

		If index < 0 Or index > Byte.MaxValue Then Throw New IndexOutOfRangeException

		If _buffer Is Nothing Then InitBuffer()
		Return _buffer(index)

	End Function

	''' --------------------------------------------------------------------------------
	''' Class.Method:	CharInitializer.Asc
	''' <summary>
	''' This is a framework compliant version of the old VB6 Asc() function
	''' </summary>
	''' <param name="character">
	''' 	The char in question. 
	''' 	Value Type: <see cref="Char" />	(System.Char)
	''' </param>
	''' <returns><see cref="Byte" />	(System.Byte)</returns>
	''' <remarks>
	''' </remarks>
	''' --------------------------------------------------------------------------------
	Public Shared Function Asc(ByVal character As Char) As Byte
		If _buffer Is Nothing Then InitBuffer()
		Return _enc.GetBytes(character)(0)
	End Function

#End Region

	Private Shared Sub InitBuffer()

		'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

End Class






