Alright, I am creating three objects in my latest project that will have allocated arrays and such during their usage, and I want to implement the "Dispose()" method for clean-up. I have already implements the proper objects, but I am curious about some of what was automatically inserted into my object code.
'The module object
Public Class moduleClass
Private disposedValue As Boolean = False
Private nameString As String
Private objectCountULong As ULong
Private objectArray() As objectClass
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
' TODO: free shared unmanaged resources
Me.disposedValue = True
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
That is the beginning of my object, and the stuff that was inserted at the end of the code. I first want to know if I need the region tags, because I'll remove them if I don't. The next thing is what the heck is it putting two Dispose() methods in the class for, and why does one have a boolean value passed to it? Seems like a waste of code if I can simply plop everything into one method. Finally, what is the difference in the two resources that the first Dispose() method mentions? All I want to do is free some arrays when the object is deleted, freed, or whatever.
Actually, I may not need to perform any cleanup. If I declare an array like "Private myArrayInt() As Integer" and then use "ReDim" to change the size while using it, will I need to use "ReDim myArrayInt(-1)" during cleanup, or will the framework automatically free this stuff? I am a C/C++ programmer used to using new/delete and malloc/free, so this VB idea of never allocating or releasing resources seems far-fetched to me. It just seems like the framework can't possibly know every array or variable that you allocate and properly free it when your application exits.