Byval vs. byref

Discussion in 'ASP .Net' started by Iams, Feb 24, 2005.

  1. Iams

    Iams Guest

    Forgive my newbie question, but I guess I don't understand passing
    parameters, in my case an array.

    I have a procedure which calls another procedure, passing an array. The new
    procedure creates the array, but when it returns to the original procedure,
    the array is lost, along with all the parameters I want modified by the
    called procedure. I thought I understood the default was byref, which would
    modify the actual variable, not just a copy. I haven't had this problem
    before. How should I do this?

    Dim arrClients as ArrayList
    Private Sub DoStuff()
    'do other stuff
    Call CreateClientArray(arrClients, i, v, q)
    End Sub

    Public Class OtherStuff
    Public Sub CreateClientArray(arrClients, i, v, q)
    arrClients.Add("Client1")
    arrClients.Add("Client2")
    i = 1
    v = "stuff"
    q = "quarter"
    end sub
     
    Iams, Feb 24, 2005
    #1
    1. Advertising

  2. Iams

    Scott M. Guest

    The default way that arguments are passed in VB.NET is ByVal not ByRef.


    "Iams" <Iams@hotmail> wrote in message
    news:...
    > Forgive my newbie question, but I guess I don't understand passing
    > parameters, in my case an array.
    >
    > I have a procedure which calls another procedure, passing an array. The
    > new
    > procedure creates the array, but when it returns to the original
    > procedure,
    > the array is lost, along with all the parameters I want modified by the
    > called procedure. I thought I understood the default was byref, which
    > would
    > modify the actual variable, not just a copy. I haven't had this problem
    > before. How should I do this?
    >
    > Dim arrClients as ArrayList
    > Private Sub DoStuff()
    > 'do other stuff
    > Call CreateClientArray(arrClients, i, v, q)
    > End Sub
    >
    > Public Class OtherStuff
    > Public Sub CreateClientArray(arrClients, i, v, q)
    > arrClients.Add("Client1")
    > arrClients.Add("Client2")
    > i = 1
    > v = "stuff"
    > q = "quarter"
    > end sub
    >
    >
     
    Scott M., Feb 24, 2005
    #2
    1. Advertising

  3. Iams

    Derek Harmon Guest

    "Iams" <Iams@hotmail> wrote in message news:...
    > I thought I understood the default was byref, which would modify the
    > actual variable, not just a copy.


    Your understanding of the difference between ByVal and ByRef is correct;
    except that the default is ByVal.

    : :
    > Public Sub CreateClientArray(arrClients, i, v, q)

    Public Sub CreateClientArray( ByRef arrClients, i, v, q)

    Here arrClients is passed-by-reference, whereas i, v and q
    are passed-by-value.

    Changes to arrClients in the Sub, CreateClientArray, affects
    the variable arrClients in DoStuff( ) [it would even affect that
    variable if it had a different name].

    If you have any other questions on the subject, this MSDN Library
    article explains everything,

    http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconArgPassMechanism.asp

    > Forgive my newbie question, but I guess I don't understand passing
    > parameters, in my case an array.


    By the way, just a gentle nudge that VB.NET questions are best raised
    in the microsoft.public.dotnet.languages.vb newsgroup. I've pointed
    follow-up to that forum because it's probably a better place to discuss
    this question than a newsgroup on ASP.NET (WebForms applications).


    Derek Harmon
     
    Derek Harmon, Feb 24, 2005
    #3
  4. Hello
    Dear Iams

    in Vb.NET parameters are by default passed by Value not by Reference,
    you should Write a function with parameters like


    Public Sub abc(ByRef a as long)
    ' ' here you parameter a is available by reference .

    end Sub


    Kind Regards
    Malik Asif



    "Iams" <Iams@hotmail> wrote in message
    news:...
    > Forgive my newbie question, but I guess I don't understand passing
    > parameters, in my case an array.
    >
    > I have a procedure which calls another procedure, passing an array. The

    new
    > procedure creates the array, but when it returns to the original

    procedure,
    > the array is lost, along with all the parameters I want modified by the
    > called procedure. I thought I understood the default was byref, which

    would
    > modify the actual variable, not just a copy. I haven't had this problem
    > before. How should I do this?
    >
    > Dim arrClients as ArrayList
    > Private Sub DoStuff()
    > 'do other stuff
    > Call CreateClientArray(arrClients, i, v, q)
    > End Sub
    >
    > Public Class OtherStuff
    > Public Sub CreateClientArray(arrClients, i, v, q)
    > arrClients.Add("Client1")
    > arrClients.Add("Client2")
    > i = 1
    > v = "stuff"
    > q = "quarter"
    > end sub
    >
    >
     
    Malik Asif Joyia, Feb 24, 2005
    #4
  5. Iams

    Scott M. Guest

    And, just to make it interesting...

    If you pass a reference type (a class for example) ByVal (the default), you
    WON'T get a copy of that object, you will get a copy of the pointer to that
    object.


    "Scott M." <> wrote in message
    news:%...
    > The default way that arguments are passed in VB.NET is ByVal not ByRef.
    >
    >
    > "Iams" <Iams@hotmail> wrote in message
    > news:...
    >> Forgive my newbie question, but I guess I don't understand passing
    >> parameters, in my case an array.
    >>
    >> I have a procedure which calls another procedure, passing an array. The
    >> new
    >> procedure creates the array, but when it returns to the original
    >> procedure,
    >> the array is lost, along with all the parameters I want modified by the
    >> called procedure. I thought I understood the default was byref, which
    >> would
    >> modify the actual variable, not just a copy. I haven't had this problem
    >> before. How should I do this?
    >>
    >> Dim arrClients as ArrayList
    >> Private Sub DoStuff()
    >> 'do other stuff
    >> Call CreateClientArray(arrClients, i, v, q)
    >> End Sub
    >>
    >> Public Class OtherStuff
    >> Public Sub CreateClientArray(arrClients, i, v, q)
    >> arrClients.Add("Client1")
    >> arrClients.Add("Client2")
    >> i = 1
    >> v = "stuff"
    >> q = "quarter"
    >> end sub
    >>
    >>

    >
    >
     
    Scott M., Feb 24, 2005
    #5
  6. Iams

    Iams Guest

    Thanks all, that explains it! And thanks for the pointer info.


    "Iams" <Iams@hotmail> wrote in message
    news:...
    > Forgive my newbie question, but I guess I don't understand passing
    > parameters, in my case an array.
    >
    > I have a procedure which calls another procedure, passing an array. The

    new
    > procedure creates the array, but when it returns to the original

    procedure,
    > the array is lost, along with all the parameters I want modified by the
    > called procedure. I thought I understood the default was byref, which

    would
    > modify the actual variable, not just a copy. I haven't had this problem
    > before. How should I do this?
    >
    > Dim arrClients as ArrayList
    > Private Sub DoStuff()
    > 'do other stuff
    > Call CreateClientArray(arrClients, i, v, q)
    > End Sub
    >
    > Public Class OtherStuff
    > Public Sub CreateClientArray(arrClients, i, v, q)
    > arrClients.Add("Client1")
    > arrClients.Add("Client2")
    > i = 1
    > v = "stuff"
    > q = "quarter"
    > end sub
    >
    >
     
    Iams, Feb 24, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Waz...
    Replies:
    0
    Views:
    426
    David Waz...
    Jul 4, 2003
  2. Goncalo

    ByVal and ByRef

    Goncalo, Dec 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    6,099
    Dmitriy Lapshin [C# / .NET MVP]
    Dec 4, 2003
  3. xamman
    Replies:
    1
    Views:
    350
    Marina Levit [MVP]
    Dec 22, 2006
  4. Rob Meade

    Byref / Byval?

    Rob Meade, Dec 28, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    791
    Rob Meade
    Dec 28, 2006
  5. ManicQin

    cTor argument byVal or byRef

    ManicQin, Aug 2, 2008, in forum: C++
    Replies:
    7
    Views:
    363
    James Kanze
    Aug 2, 2008
Loading...

Share This Page