Results 1 to 5 of 5

Thread: ByConfusion

  1. #1

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    ByConfusion

    I've been reading about and writing practice procedures here lately and I still have some confusion over the difference between ByVal and ByRef

    I understand that ByVal is used to pass a value as an arguement into my procedure. But whats the difference in using ByRef? From what I understand you use ByRef if the arguement will change and you can return that arguement from your procedure. Couldn't you just do that with a function? I'm lost

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  2. #2
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    Re: ByConfusion

    If you pass a variable ByVal then you are simply creating a copy of the variable to pass to the function. If you pass a variable ByRef then you are actually referencing the memory location of the variable. The main implication of this is that if you pass the variable ByRef then the procedure will not have any affect on the information stored in memory. If you pass ByRef then the procedure may actually change the information that is stored in memory. As a side note, ByRef is also faster.
    "Imagination is more important than knowledge..."

    Albert Einstein
    -----------------------------------------------
    If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,437

    Re: ByConfusion

    What nothingof value says is not strictly true. ByVal and ByRef refer to how you pass a variable. When you are talking about variables it always means that value types and reference types will behave differently. That's because value type variables contain the object itself while reference types contain the memory address of an object.

    If you pass a variable ByVal then any changes you make to that variable WILL NOT be reflected in the original variable after the method completes. If you pass a variable ByRef then any changes you make to that variable WILL be reflected in the original after the method completes. Having said that, it is importantt to understand what making a change to a variable means. Assigning an object to a variable is making a change to that variable. Setting a property of a variable is NOT making a change to that variable, but rather making a change to the object that the variable refers to. That means that if you pass a reference type object, i.e. an instance of a class, ByVal then set a property of that object within the method, that change WILL be reflected after the method completes. That's because a copy of the original variable was passed to the method, but that copy still refers to the same object as the original variable. If you don't fully understand the difference between value types and reference types then you cannot understand this difference either.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,235

    Re: ByConfusion

    NothingofValue managed a truly unfortunate typo

    However, I can add one small bit of value to the two replies above. How is it different from a function? Actually, it doesn't have to be in effect, however, what if you have a function that has to both return an integer, as well as a boolean indicating whether or not is succeeded? You could create a structure containing both the integer and a boolean, and return that, or you could return a boolean, and pass in an integer ByRef. Altering the argument that was passed in ByRef would be the integer return, while the return value of the function would be the boolean return.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: ByConfusion

    Quote Originally Posted by jmcilhinney
    If you don't fully understand the difference between value types and reference types then you cannot understand this difference either.
    I must not fully understand this because I'm having a hard time grasping the concept of ByRef

    The examples I've been reading use this procedure:
    VB Code:
    1. Sub AddName(ByVal Team As String, ByRef ReturnString As String)
    2.         Dim Prompt, Nm, WrapCharacter As String
    3.         Prompt = "Enter a " & Team & " employee."
    4.         Nm = InputBox(Prompt, "Input Box")
    5.         WrapCharacter = Chr(13) + Chr(10)
    6.         ReturnString = Nm & WrapCharacter
    7.     End Sub

    and then this buttonclick handler to add the desired Input in a multiline textbox.
    VB Code:
    1. Private Sub btnSales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSales.Click
    2.         Dim SalesPosition As String = ""
    3.         AddName("Sales", SalesPosition)
    4.         txtSales.Text = txtSales.Text & SalesPosition
    5.     End Sub

    I understand completely whats happening here, except for a certain degree of the ByRef arguement.

    If I'm guessing right, you pass the procedure a blank string (because it has to have something as an arguement) then the procedure changes the value of the arguement (based on the InputBox input), and then the program can reference that arguement

    Whew!

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

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