Alright, I am working on a small class that I can reuse to log errors in my VB .NET applications, but am not sure how to achieve what is fairly easy for me in C++. I want to specify the maximum number of errors to log upon object initialization and then record class names and the errors they specify when errors occur, which will vary in length. Maybe some code will help.
Public Class errorClass
Private iBufferSize, iErrorCount As Integer
Private strBuffer()() As String
' Public method to initialize this object
Public Function Initialize(ByRef iSize As Integer) As Boolean
' Verify that this object is not already initialized
If Me.bInitialized Then Return False
' Validate the buffer size
If iSize < 4 Or iSize > 1024 Then Return False
' Initialize the object and return
Me.iBufferSize = iSize
Me.iErrorCount = 0
ReDim Me.strBuffer(Me.iBufferSize)
Me.bInitialized = True
Return True
End Function
' Public method to add an error to the buffer
Public Function AddError(ByRef objClass As Object, ByRef strValue As String) As Boolean
Dim iLoop As Integer
' Purge the last error if the buffer is full
If Me.iErrorCount = Me.iBufferSize Then
Me.strBuffer(0) = Nothing
End If
' Loop through and move each error up one level
For iLoop = 1 To Me.iBufferSize Step 1
Me.strBuffer(iLoop - 1) = Me.strBuffer(iLoop)
Next iLoop
' Now add the new error
Me.strBuffer(Me.iBufferSize - 1) = Nothing
Me.strBuffer(Me.iBufferSize - 1) = New String() {strValue}
' Increment the error counter and return
If Me.iErrorCount < Me.iBufferSize Then Me.iErrorCount += 1
Return True
End Function
End Class
Am I doing this the correct way for VB .NET? I am not sure if I am freeing the oldest error properly once the buffer is full, and I am not sure if I am freeing the new error position properly. I am used to using "free()" or "delete" depending on the situation in C or C++. I can't get used to the idea of memory magically freeing itself and it gives me hell in VB .NET...
-
Sephiroth