.NET Framework Bookmark and Share   
 index > Regular Expressions > Passing object byval to function to Dispose?
 

Passing object byval to function to Dispose?

I am passing anobject toa common function to dispose it.

Public Shared Sub ReleaseObject(ByVal reader As SqlDataReader)

If Not IsNothing(reader) Then

If Not reader.IsClosed Then

reader.Close()

End If

reader = Nothing

End If

End Sub


Since I am passing thereaderobject byVal to this function. Iam still able to accessreader object in my calling class after my call to ReleaseObject.

calling code

ReleaseObject(rd)
';;
'just to check the object is disposed
debug.write (rd.Depth.ToString)
';;

I know following thing about parameter passing.
Reference Type.
1. If you pass ByVal you are creating a copy of the pointer to the object in the stack. both pointers point to a single instance of the object in the heap.

2. If you pass ByRef, you are using the original pointer in the stack.

Value Type
3. If you pass ByVal , you are creating a copy of the variable in the stack. (changes will not affect original variable)

4. If you pass ByRef, you are using the original value in the stack. ( changes will affect original variable)

I also know that setting a objectto nothing, will not release any memory
It justhelper the developer ... by throwing an exception if he used those object after disposing


My Question is whether it has any performance or memory issue in passing an object to dispose to a function by value?

like

dim r as new sqlDataReader
ReleaseObject(r)
'object is passed by value, i can still use
debug.write (r.depth)


Any thoughts will be helpful.

Thank you
Riju K K

LifeInsideMatrix
If you pass an object by value to a dispose function the function will dispose a copy of the object so the original object will be untouched.

Dim r as MyObject

Dispose(r) 'if the Dispose method take the argument by value it will create a new MyObject inside it and will dispose the copy.

However, if you pass the parameter ByRef it will be affected by this method.

So if you want to dispose the object you must pass it by reference. There is still one more trick: you can't set it to NULL inside that method because if you pass an object by reference its pointer is passed by value so setting it to NULL will not affect the outside of method object.

In .NET, most of the time, you don't have have to manually dealocate the memory because the GarbageCollector will do that for you

VictorHurdugaci

You can use google to search for other answers

Custom Search

More Threads

• Visual Basic 2008 MSHTML To Parse Text, Visual Basic 2008 MSHTML To Extract Links Text etc..
• Need help with a negative lookahead, I think
• How to put double quotes around attributes
• ANATEL TXT - Regular Expression
• .Net Fonts problem
• RegexOptions.Compiled
• Regular expression issue
• i\r \n are not taken into consideration in my regex
• Regular Expression
• Foreign Characters