Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: ByVal / ByRef

  1. #11
    Member
    Join Date
    2015-11
    Posts
    38
    Login to Give a bone
    0

    Default Re: ByVal / ByRef

    Thanks for your replies:

    Jeff - Yea, I think im getting it now.

    Kerry - Im not 100% certain here, but, i think the key word 'object' in jeff's post means that what he is saying is correct, because, objects are reference types and appear to be passed ByRef only.

    As for the microsoft link - im not getting it - I dont understand the meaning of;
    The procedure cannot change the variable but can change members of the instance to which it points.
    Im probably being terribly thick here but in what way can you 'change the variable' if its not by changing its value and/or member values??

    In my starting post I am passing the list ByVal which,acording to the table, means i cant change the variable but can change the variable's members. Well the list variable changes contents - so what exactly am i changing here. I thought it was the variable i.e. its value....so confused.

    I suppose its not that much of an issue because its not hard to avoid problems relating to passing objects. You just make a clone/copy to operate on. BUT I think I'm missing something fundamental here so if someone can enlighten me I would be very greatfull.

    Regards

    Matt

  2. #12
    AUGI Addict sinc's Avatar
    Join Date
    2004-02
    Location
    Colorado
    Posts
    1,986
    Login to Give a bone
    0

    Default Re: ByVal / ByRef

    The key thing is that there are two different types, "value types" and "reference types". And either type can be passed either ByVal or ByRef. So there's four different things that can happen - passing a value type ByVal, passing a value type ByRef, passing a reference type ByVal or passing a reference type ByRef.

    For the difference between these four things, take a look at this, and see if it helps any:

    http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx

    For objects you create in your code, you can also create either value types or reference types. Structs are value types, and Classes are reference types. So if you need to create an object that behaves like a value type (such as the Point3d in the Autocad API), you can use a Struct instead of a Class.

  3. #13
    Member
    Join Date
    2015-11
    Posts
    38
    Login to Give a bone
    0

    Default Re: ByVal / ByRef

    Thanks sinc,

    Its becoming clearer - although that's a C# link.

    When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block.
    This cleared up the question of changing the variable and changing its members.

    I do have a question - what would be the same as the 'out' keyword in VB.NET ?

    Regards

    Matt

  4. #14
    AUGI Addict sinc's Avatar
    Join Date
    2004-02
    Location
    Colorado
    Posts
    1,986
    Login to Give a bone
    0

    Default Re: ByVal / ByRef

    With .NET, it's a good idea to get used to being able to read C# and apply what you see to VB. The two languages are very similar, so it's usually pretty easy to get the idea of what the example is doing, even if it's in C#.

    I don't work with VB myself - I've never liked Basic syntax. But I believe in VB, "Out" doesn't really apply, because VB automatically initializes every variable as soon as it is declared. In C#, you can declare a variable without initializing it, but you can't in VB, so I don't think the "Out" keyword comes into play in VB.

    I found this page, which shows a bunch of equivalent code in VB and C#. It might be useful:

    http://www.harding.edu/fmccown/vbnet...omparison.html

  5. #15
    Active Member
    Join Date
    2009-08
    Posts
    93
    Login to Give a bone
    0

    Default Re: ByVal / ByRef

    http://books.google.com/books?id=IO4...proach&f=false


    go to page 71 when it says module do not think of a module in VB in this context it is a function
    Last edited by Jeff H; 2010-09-01 at 07:35 AM.

  6. #16
    Member
    Join Date
    2015-11
    Posts
    38
    Login to Give a bone
    0

    Default Re: ByVal / ByRef

    Thanks Sinc & Jeff

    Becoming clear now.


    Matt.

  7. #17
    Active Member
    Join Date
    2009-08
    Posts
    93
    Login to Give a bone
    0

    Default Re: ByVal / ByRef

    Correct me if I am wrong,
    The way I think about it is when you use pass by value you send the the actual value to calling module and then the computer creates a new memory address and stores the value.
    When you pass by reference the computer sends the memory address.

    So when variable1 = List: {A,B,C} at memory address 2000

    Vb will pass all objects by reference

    When the computer processes variable1 = variable2

    It is not sending the list it telling the computer for variable2 it's memory address is at 2000

    So any changes made on variable1 or variable2 the values are stored and changed at memory address 2000.

    To make a copy independent of the variables you must get the values from memory address 2000 and create new variable3 and create a new memory adress to store the value.

    variable3 ={A,B,C} at memory address 2001

    If that helps at all.

Page 2 of 2 FirstFirst 12

Posting Permissions

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