J
Joop
Hello,
I've written the following little helper function which calls Dispose on
classes which implement the idisposable interface.
Public Sub DisposeObject(ByVal DisposeAbleObject As IDisposable)
If Not DisposeAbleObject Is Nothing Then
DisposeAbleObject.Dispose()
End If
End Sub
The reason for this is to avoid ugly looking finally clauses with lots
of checking if an object ref is nothing before calling dispose on them.
For example in a try finally block in which a connection is opnened, a
transaction is started and an sqldatareader gets created which looks
like this:
try
......
finally
if sqlconnection is nothing then
sqlconnection.close
end if
if Transaction is nothing then
Transaction.dispose
end if
if Datareader is nothing then
Datareader.close
end if
end try
i'd write this:
try
.....
finally
disposeobject(sqlconnection)
disposeobject(transaction)
disposeobject(sqldatareader)
end try
a lot better looking IMO. I dont have to worry about whether the ref is
nothing or not.
Now my question is this: Is this going to cause any strange behavior?
I've tested it and it seems to work.. but im wondering if there could be
any side effects since im not calling the specific Close of the
datareader or sqlconnection, im calling the Dispose. The documentation
says there's not much difference in calling Dispose instead of Close on
a connection, but can i assume the same for any object that implements
IDisposable?
Thanks
I've written the following little helper function which calls Dispose on
classes which implement the idisposable interface.
Public Sub DisposeObject(ByVal DisposeAbleObject As IDisposable)
If Not DisposeAbleObject Is Nothing Then
DisposeAbleObject.Dispose()
End If
End Sub
The reason for this is to avoid ugly looking finally clauses with lots
of checking if an object ref is nothing before calling dispose on them.
For example in a try finally block in which a connection is opnened, a
transaction is started and an sqldatareader gets created which looks
like this:
try
......
finally
if sqlconnection is nothing then
sqlconnection.close
end if
if Transaction is nothing then
Transaction.dispose
end if
if Datareader is nothing then
Datareader.close
end if
end try
i'd write this:
try
.....
finally
disposeobject(sqlconnection)
disposeobject(transaction)
disposeobject(sqldatareader)
end try
a lot better looking IMO. I dont have to worry about whether the ref is
nothing or not.
Now my question is this: Is this going to cause any strange behavior?
I've tested it and it seems to work.. but im wondering if there could be
any side effects since im not calling the specific Close of the
datareader or sqlconnection, im calling the Dispose. The documentation
says there's not much difference in calling Dispose instead of Close on
a connection, but can i assume the same for any object that implements
IDisposable?
Thanks