Multidimensional jagged array of strings...

SephirothSephiroth Fayetteville, NC, USA
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...


  • Unless I missed something while reading your code you can do everything that code does with a simple generic string list. I "cut my teeth" on C++ myself so I see what you're doing in that code, but none of that is necessary with VB.NET. You can create a list of string objects to hold the errors without all the buffer business:

    Dim errors As New List(Of String)()
    errors.Add("Some Error")

    The List object will append whatever string you want to the collection and handle the memory management behind the scenes for you. You don't have to allocate more memory space or limit yourself to a particular number of bytes. You also don't have to free() or delete memory. All of this is handled for you.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion