Armin Zingler said:
No need to apologize. You're welcome! Only very few people here have
their,
let's say "special" opinion about VB.Net - which is acceptable - but
unfortunatelly that is often expressed in unfounded allegations, twisting
one's words or laying into someone the nasty way.
May I ask what you're trying with the Byte array? I have doubts because
it seems to imply (time consuming) character en/decoding.
Hi Armin. Just to clarify what I am doing. The files I build (bank
transaction files) must contain n characters per row (eg 120) with each row
comprising of n fixed length fields.
Generally I can just roll along using a pointer to my position in the row
and effectively append field after field. There are time however when I need
to skip some optional fields and then write data further along. The reverse
of this is that I also need to parse files of this type extracting field
values at various positions.
The Mid Function and Statement serve this purpose admirably, however, I have
always been curious as to whether there was a more efficient method. I am
aware that both VB6 and VB.Net have the Mid Function/Statement and I was
also aware that the VB.Net Mid was quite a bit slower.
Tom Shelton's figures tell me that this is probably insignificant as the
bank files I produce typically would only have 3-4k rows. As I mentioned,
the whole thing just becomes academic when we can see the insignificant
differences that the test results produce in the 3-4k range.
The Byte array idea was to simply try something else. Here is the code
(sorry, have to include the whole class). I have not yet tested this:
Public Class StringBuffer
Implements IDisposable
#Region " Variables "
Private Const M As String = "StringBuffer-"
Private Const SEP As String = "-"
'----------------
'internal storage
'----------------
Private _buffer As Byte()
Private _encoding As New System.Text.ASCIIEncoding()
'----------------------------------
'points to current postion in array
'----------------------------------
Private _pointer As Integer
#End Region
#Region " Properties "
Public ReadOnly Property Pointer() As Integer
Get
Return _pointer
End Get
End Property
#End Region
#Region " Methods "
''' <summary>
''' Write text from current pointer position
''' </summary>
''' <param name="sText"></param>
''' <returns>Boolean</returns>
''' <remarks>pointer increments after each write</remarks>
Public Function Write(ByVal sText As String) As Boolean
Try
Dim temp() As Byte = _encoding.GetBytes(sText)
For i As Integer = 0 To (temp.Length - 1)
_buffer(_pointer) = temp(i)
'--------------------------------
'we need to increment our pointer
'--------------------------------
_pointer += 1
Next
Return True
Catch ex As Exception
Throw New Exception(M &
System.Reflection.MethodInfo.GetCurrentMethod.Name & SEP & ex.Message)
End Try
End Function
''' <summary>
''' Write text from nominated start position
''' </summary>
''' <param name="StartPos"></param>
''' <param name="sText"></param>
''' <returns>Boolean</returns>
''' <remarks></remarks>
Public Function Write(ByVal StartPos As Integer, ByVal sText As String)
As Boolean
Try
Dim temp() As Byte = _encoding.GetBytes(sText)
For i As Integer = 0 To (temp.Length - 1)
_buffer(StartPos + i) = temp(i)
Next
Return True
Catch ex As Exception
Throw New Exception(M &
System.Reflection.MethodInfo.GetCurrentMethod.Name & SEP & ex.Message)
End Try
End Function
''' <summary>
''' Read n characters from nominated start position
''' </summary>
''' <param name="StartPos"></param>
''' <param name="nChars"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Read(ByVal StartPos As Integer, ByVal nChars As Integer)
As String
Try
Return _encoding.GetString(_buffer, StartPos, nChars)
Catch ex As Exception
Throw New Exception(M &
System.Reflection.MethodInfo.GetCurrentMethod.Name & SEP & ex.Message)
End Try
Return String.Empty
End Function
Public Overrides Function ToString() As String
Try
Return _encoding.GetString(_buffer)
Catch ex As Exception
Throw New Exception(M &
System.Reflection.MethodInfo.GetCurrentMethod.Name & SEP & ex.Message)
End Try
Return String.Empty
End Function
#End Region
#Region " IDisposable Support "
Private disposedValue As Boolean
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
_buffer = Nothing
_pointer = Nothing
_encoding = Nothing
End If
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
#Region " Constructors "
Public Sub New(ByVal BufferLength As Integer)
ReDim _buffer(BufferLength - 1)
End Sub
Public Sub New(ByVal BufferLength As Integer, ByVal CharType As Char)
Dim temp As New String(CharType, BufferLength)
_buffer = _encoding.GetBytes(temp)
End Sub
Public Sub New(ByVal sBuffer As String)
_buffer = _encoding.GetBytes(sBuffer)
End Sub
#End Region
End Class