Please have a look at the code below:
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Data.OracleClient
Public Class clsParameterValues
Implements IDisposable
Private paramValues(0) As DbParameter
Public Function AssignParameterValues(ByVal strParameterName As String, ByVal strParameterValue As String, ByVal intDatabaseType As Integer) As Integer
Dim intArrayBound As Integer
intArrayBound = UBound(paramValues)
If intArrayBound > 0 Then
ReDim paramValues(intArrayBound)
End If
If intDatabaseType = 1 Then
paramValues(intArrayBound) = New SqlParameter(strParameterName, strParameterValue)
ElseIf intDatabaseType = 2 Then
paramValues(intArrayBound) = New OracleParameter(strParameterName, strParameterValue)
'paramValues(intArrayBound) = New OracleParameter(":" & strParameterName, OracleType.Int32)
'paramValues(intArrayBound).Value = strParameterValue
End If
Return intArrayBound
End Function
Public Function getParameterValues() As DbParameter()
Return paramValues
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Erase paramValues
paramValues = Nothing
End Sub
End Class
The webpage function looks like this:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objParameterValues As New clsParameterValues
Using objParameterValues
'Use the objParameterValues class here.
End Using
objParameterValues=nothing
End Using
End Sub
I am using IDisposable.Dispose to erase the array before setting it to Nothing. I believe this is bad practice because the Array class does not implement IDisposable. Is it even necessary to erase an array and set it to Nothing? (Does the garbage collector deal with this?)
The Erase statement dates from old versions of Basic, the kind where manual memory management was useful. No more, memory management is automatic in .NET. It is still supported for compatibility reasons. All it does is set the array reference to Nothing. So your code is equivalent to:
So no point to it. You should never implement IDisposible to set a variable to Nothing, that’s not the interface’s contract. A disposed object may never be used again. So no point in setting the array reference to null since that doesn’t actually do anything to the real array object on the garbage collected heap. I cannot otherwise see a scenario where you would want to help, any clsParameterValues object should have a limited lifetime. It just isn’t useful anymore when you null the array reference.
Just remove the IDisposable implementation.