Results 1 to 13 of 13

Thread: Byref and ByVal

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    155

    Byref and ByVal

    Hi:
    Can someone tell me When to use ByVal? If Byref supposed to be able to the task, then why need Byval?

    I read a lot of article about this, but I'm yet to find an answer!


    Thanks

    Simon

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Byref and ByVal

    Just to summarise, you should pass by value by default and only pass by reference when it's specifically required. It would only be specifically required if you wanted to be able to assign a new value or object to the parameter within the method and have the original variable contain that new value or refer to that new object after the method completes.

    When you pass by value a copy of the variable is created and passed, while when you pass by reference a reference to the variable is passed. In VB6 it was the default to pass by reference to avoid copying the contents of variables that might be large. In .NET, if a variable contains a large amount of data then it must be a poorly designed structure. No well designed structures should be so large that copying them should be an issue. Anything large enough to be considered an issue should be a class, in which case the variable contains only a memory address and copying it is no issue at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Byref and ByVal

    ByRef and ByVal are VB keywords. In C# we use 'ref' to pass arguments by reference. Passing by value is the default and thus implicit when 'ref' or 'out' are omitted.

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Byref and ByVal

    C# is criticized for its lack of support for optional parameters. I am not to sure but i have been told you should use a temp variable. Could someone please clarify?

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Byref and ByVal

    If anything VB should be criticized for it's support of optional parameters. Personally, I think overloading is the preferable way.

    Other than that, Huh? Temp vars?, and what does that have to do with this thread in the first place.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Byref and ByVal

    VB.NET is criticized for its support of optional parameters.

    Features in VB.NET not in C#:
    Optionally ignore ref/ByRef behavior for passing arguments. (C# requires a temp variable to do this.)

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Byref and ByVal

    Quote Originally Posted by Hell-Lord
    VB.NET is criticized for its support of optional parameters.

    Features in VB.NET not in C#:
    One has nothing to do with the other. In VB.NET you can enclose a variable in parentheses when passing it to force it to be passed by value, even if the method argument is declared by reference. This is because VB.NET will implicitly create a temporary variable and pass that. Try this code:
    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Dim var1 As Integer = 10
    3.     Dim var2 As Integer = 100
    4.  
    5.     MessageBox.Show(String.Format("var1 = {0}; var2 = {1}", var1, var2))
    6.  
    7.     Me.ChangeParameterValue(var1)
    8.     Me.ChangeParameterValue((var2))
    9.  
    10.     MessageBox.Show(String.Format("var1 = {0}; var2 = {1}", var1, var2))
    11. End Sub
    12.  
    13. Private Sub ChangeParameterValue(ByRef p As Integer)
    14.     p *= 2
    15. End Sub
    See that var1's value is changed because it is passed by reference, while var2's value is not because it is forced to be passed by value. In C# you have to create that temp variable explicitly:
    C# Code:
    1. private void Form1_Load(object sender, EventArgs e)
    2. {
    3.     int var1 = 10;
    4.     int var2 = 100;
    5.  
    6.     MessageBox.Show(String.Format("var1 = {0}; var2 = {1}", var1, var2));
    7.  
    8.     this.ChangeParameterValue(ref var1);
    9.  
    10.     int var3 = var2;
    11.  
    12.     this.ChangeParameterValue(ref var3);
    13.  
    14.     MessageBox.Show(String.Format("var1 = {0}; var2 = {1}", var1, var2));
    15. }
    16.  
    17. private void ChangeParameterValue(ref int p)
    18. {
    19.     p *= 2;
    20. }
    None of that has anything whatsoever to do with optional parameters or overloading.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Byref and ByVal

    Thanks for clearing that up

  10. #10
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Re: Byref and ByVal

    Quote Originally Posted by jmcilhinney
    When you pass by value a copy of the variable is created and passed
    I think a reference to the object, if it is a reference object, is passed to the method, then a copy of the object is created within the method.
    I'm trawling deeply through my memory bank here, but I think all parameters are actually references to objects, the value of the reference is retrieved within the implementation of the method.

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Byref and ByVal

    If you pass a reference by value the reference is copied. If you pass a reference by reference a reference to the reference is passed.

    To actually copy an object it needs to provide a Clone method and it won't happen implicitly.

  12. #12
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Re: Byref and ByVal

    Quote Originally Posted by penagate
    If you pass a reference by value the reference is copied. If you pass a reference by reference a reference to the reference is passed.

    To actually copy an object it needs to provide a Clone method and it won't happen implicitly.
    So the objects themselves are never passed, only references to them, that is what I thought.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Byref and ByVal

    A method parameter is a special variable and has a special place in a stack frame, which is the area of memory that basically defines a method call that is constructed every time you call a method. That parameter can contain one of four things:

    1. A value, if the parameter is a value type passed by value.
    2. A reference to a value, if the parameter is a value type passed by reference. In C/C++ this would be a pointer.
    3. A reference, if the parameter is a reference type passed by value. In C/C++ this would be a pointer.
    4. A reference to a reference, if the parameter is a reference type passed by reference. In C/C++ this would be a pointer to a pointer.

    If it's a value then changing that value has no effect on the original.

    If it's a reference to a value then changing the value of the parameter means changing the value of the variable referred to by the parameter, which does affect the original.

    If it's a reference then changing a property of the parameter means changing a property of the object referred to by the parameter, which is the same object referred to by the original variable, so that does affect the original. Assigning a new object to the parameter does not assign a new object to the original variable though, so that change doesn't affect the original.

    If it's a reference to a reference then there's still only one object so changing a property does affect the original. Assigning a new object to the parameter means assigning a new object to the variable referred to by the parameter, which is the original variable, so this change does affect the original.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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