請看看下面的代碼:擦除數組 - 是否有必要?
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
該網頁的功能如下:
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
我使用IDisposable.Dispose
將其設置爲Nothing
前刪除陣列。我相信這是不好的做法,因爲Array類不實現IDisposable
。是否有必要擦除陣列並將其設置爲Nothing
? (垃圾回收器是否處理這個問題?)
在相關的一面註釋中,如果您的實例數據(或者您自己擁有一些非託管資源,例如操作系統句柄),則應該只使用「IDisposable」。否則不需要。 – tcarvin