Results 1 to 7 of 7

Thread: [RESOLVED] ByVal in Function Arguments

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Resolved [RESOLVED] ByVal in Function Arguments

    If this:
    VB Code:
    1. Private Function MyFunc(ByVal sTest As String)
    The same as this:
    VB Code:
    1. Private Function MyFunc(sTest As String)
    Or does the second one mean ByVal/ByRef and require more CPU time? The reason I ask is that I have quite a few arguments in the function I am making and ByVal infront of all of them makes it very long when the AutoFill text pops up.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: ByVal in Function Arguments

    ByVal means that the function can change the value of the variable. ByRef means that a new instance is invoked, and destroyed when the function ends.
    Might take a bit longer to invoke, but you would also save time not having to copy that variable back after the function ends.

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: ByVal in Function Arguments

    Thank dglienna, but I need to know what happens if ByVal and ByRef are ommited. It it treated like ByVal automatically or is it somewhat like variant and take up more CPU time?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: ByVal in Function Arguments

    dglienna: a bit incorrect. ByVal just means that a copy of the variable is passed to the procedure. ByRef means that the original variable is passed to the procedure. This means that when you change a ByRef variable in the procedure (be it a function or a sub), the passed variable changes as there is no copy of the variable. As far as I know, ByVal is slightly slower than ByRef; I've never tested it really (or if I have, I've forgotten the results long time ago).

    I think ByRef was used by default. I'm not sure though, I tend to always use ByRef and ByVal so I can be sure of which it does But yes, it is either ByVal or ByRef, nothing other special.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: ByVal in Function Arguments

    The second example is passing the variable "ByRef" and not ByVal. The default is ByRef when nothing is designated.
    Although it may seem easier to not include the ByVal, it actually is a completely different meaning. ByVal is like passing in a
    copy of the variable to your function. At the calling line of code the passed value can not be retrieved.

    However if you use ByRef then you are only passing a link that represents the variable and any changes to the variable are reflected
    back.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: ByVal in Function Arguments

    byVal is the default in VB.Net

    That's a change from VB6 (and prior).

    [edit] So specify it (so conversion to .Net is easier)...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: ByVal in Function Arguments

    Oops. I didn't read it closely enough. The last sentence says can't. Sorry.
    by value
    A way of passing the value, rather than the address, of an argument to a procedure. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.
    Here's byref

    by reference
    A way of passing the address, rather than the value, of an argument to a procedure. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed.

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