Custom Class Disposal

et_

Newcomer
Joined
Feb 16, 2007
Messages
15
If i create a small class:

Code:
Public Class XMLNode1
    Private xmlHeader As String
    Private xmlValue As String

    Public Sub New()
        xmlHeader = Nothing
        xmlValue = Nothing
    End Sub
    Public Sub New(ByVal head As String, ByVal val As String)
        Me.propHead = head
        Me.propVal = val
    End Sub

    Public Property propHead()
        Get
            Return xmlHeader
        End Get
        Set(ByVal value)
            xmlHeader = value.ToString
        End Set
    End Property
    Public Property propVal()
        Get
            Return xmlValue
        End Get
        Set(ByVal value)
            xmlValue = value.ToString
        End Set
    End Property


End Class

How should i go about writing a disposal method for it?
Also, my idea of disposing a class is releasing all th eobject that class uses...Is that correct?


Please help :confused:
 
Unless your class deals with non-managed resources (or .net wrappers around them such as file streams or data base connections) then you don't need to worry about disposing of resources. If the code posted is the entire class then as it only contains two strings there isn't any need to worry about disposing of it's resources.

Just as a side note though you might want to give the two properties a valid data type - as it stands the are just going to be treated as object rather than strings.
 
I do have another class that uses an arraylist,xml reader, sql adapter, and sql connection.

I read about including a dispose method , but im not sure how to implement this. I guess i could just call each objects dispose method, but im curious as to add the disposal methods to my class.
 
If you are only using the data adapter to access the db then just let it manage the connection for you, i.e. don't open or close it yourself.

If you are using the connection directly (DataReader or executing commands directly) then just get into the habit of opening the connection as you need it and closing it as soon as you are finished (preferably with a try ... finally or a using block).

Again either of these methods mean you shouldn't need to deal with the disposal of objects directly. If you are interested in how to write a dispose method then either http://msdn2.microsoft.com/en-us/library/system.idisposable.aspx or http://msdn.microsoft.com/msdnmag/issues/07/07/CLRInsideOut/default.aspx are worth a read.
 
Back
Top