ByRef the ByVal way ??
-
I have an XmlDocument (System.Xml) that i want to add to and do various other stuff but i want to split the routines down. I've read that ByVal is 'safer' to use and possibly quicker than byref so i am proposing to layout my functions as below... Imports System.Xml 'Declaration Dim XmlDocObject as New XmlDocument XmnlDocObject = InitialiseXml(XmlDocObject) Private Function InitialiseXml(ByVal v_XmlDocObject as XmlDocument) as XmlDocument 'Temp object Dim TempXmlObject = v_xmlDocObject 'Do something with temp object...... 'Return object now processing is finished Return TempXmlObject 'Destroy temp object TempXmlObject = Nothing End Function What i would like to know is if the above is good / bad practice? Anything thing wrong with this way? Is this the preferred way as opposed to using Byref or am i creating extra objects which will lie around in memory (until GC see fit to get rid)? Regards
-
I have an XmlDocument (System.Xml) that i want to add to and do various other stuff but i want to split the routines down. I've read that ByVal is 'safer' to use and possibly quicker than byref so i am proposing to layout my functions as below... Imports System.Xml 'Declaration Dim XmlDocObject as New XmlDocument XmnlDocObject = InitialiseXml(XmlDocObject) Private Function InitialiseXml(ByVal v_XmlDocObject as XmlDocument) as XmlDocument 'Temp object Dim TempXmlObject = v_xmlDocObject 'Do something with temp object...... 'Return object now processing is finished Return TempXmlObject 'Destroy temp object TempXmlObject = Nothing End Function What i would like to know is if the above is good / bad practice? Anything thing wrong with this way? Is this the preferred way as opposed to using Byref or am i creating extra objects which will lie around in memory (until GC see fit to get rid)? Regards
Your code is incorrectly written so it really doesn't matter which one you use. What you've done is definitely bad practice. No, ByVal is not "safer", nor is it "quicker". The two are used for different reasons. The way you're writing your code, you're writing a function that returns an XmlDocument, the one that's passed in, and you don't have to. Since you're passing in a Reference type (any class instance), ByVal passes the reference to the object, not the object itself. Passing in an object ByRef passes in a reference to the reference of the object, which doesn't really give you anything. So, since your function is getting a reference to the object on the heap, it can modify that object and not return anything.
Dim XmlDocObject As New XmlDocument InitializeXml(XmlDocObject) Private Sub InitializeXml(ByVal xmlDocument As XmlDocument) ' Do whatever you need to do to "xmlDocument" ' Do not "return" anything since you're modifying ' the original object. End Sub
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Your code is incorrectly written so it really doesn't matter which one you use. What you've done is definitely bad practice. No, ByVal is not "safer", nor is it "quicker". The two are used for different reasons. The way you're writing your code, you're writing a function that returns an XmlDocument, the one that's passed in, and you don't have to. Since you're passing in a Reference type (any class instance), ByVal passes the reference to the object, not the object itself. Passing in an object ByRef passes in a reference to the reference of the object, which doesn't really give you anything. So, since your function is getting a reference to the object on the heap, it can modify that object and not return anything.
Dim XmlDocObject As New XmlDocument InitializeXml(XmlDocObject) Private Sub InitializeXml(ByVal xmlDocument As XmlDocument) ' Do whatever you need to do to "xmlDocument" ' Do not "return" anything since you're modifying ' the original object. End Sub
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thanks, i'm currently upgrading from VB6 to .net and want to start off on a firm footing employing some good practices. I agree with Dave's response, anybody else with an opinion more than welcome though. Many Thanks