Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. ByRef the ByVal way ??

ByRef the ByVal way ??

Scheduled Pinned Locked Moved Visual Basic
xmlperformancequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    barney_1972
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • B barney_1972

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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

        B Offline
        B Offline
        barney_1972
        wrote on last edited by
        #3

        Thanks, 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups