VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Multidimensional jagged array of strings... Posted by Sephiroth on 4 Aug 2010 at 9:29 AM
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
Report
Re: Multidimensional jagged array of strings... Posted by asponge on 26 Aug 2010 at 9:48 PM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.