Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Multidimensional jagged array of strings...

SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
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.
[code]
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
[/code]
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...
-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • aspongeasponge Posts: 42Member
    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:

    [code]
    Dim errors As New List(Of String)()
    errors.Add("Some Error")
    [/code]

    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.