CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: byref or byval

  1. #1
    Join Date
    Jul 2004
    Location
    Jakarta, Indonesia
    Posts
    596

    byref or byval

    i want to ask..

    if i'm going to make a sub module..what is the best method to passing parameter...byval or byref?

    thanks,
    erick

  2. #2
    Join Date
    Sep 2004
    Posts
    292

    Re: byref or byval

    i prefer byval so the passed value would not be changed by th caller and will not cause strange changes to your variables, but its my own opinion.

  3. #3
    Join Date
    Aug 2004
    Location
    INDIA
    Posts
    260

    Re: byref or byval

    Hi,

    It depends on your parameter usuage within your function defintion. Usually by referrence is used when you need to change the parameter within your function defintion.

    Pass by value shall be used when you want to read parameter data information and use that info within your function. It has a overload that it calls copy constructor of your passed object. If you are sure that you dont want to modify the parameter object inside your function you can make passing parameter as immuttable. (const Object &Obvar .).
    If you feel this post is useful,
    Rate this Post by clicking right top corner (Rate this Post)
    Santhosh

    ***Add strength to your country - Interesting Poll
    ***

  4. #4
    Join Date
    Jul 2004
    Location
    Jakarta, Indonesia
    Posts
    596

    Lightbulb Re: byref or byval

    thanks for the information

    it's not always better byval or byref..it depends on the situation..

    thanks

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL

  5. #5
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: byref or byval

    Indeed, basically if you don't need to pass them ByRef, then pass them ByVal.
    Unlike one would think, ByVal is actually faster than ByRef because it does not need to pass back the data in the parameter(s) you passed. In VB, evertything is passed ByRef instead of ByVal, unless otherwise specified. This is done because some datatypes cannot be passed byval, such as Objects.

    Anyway, it's a good practice to always explicitly define whether or not a value is passed ByRef or ByVal. Not only will it be more efficient, it will also be easier for others to understand your code, since they can directly see wheter or not you are going to change the data that has been passed to the procedure.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured