Results 1 to 3 of 3

Thread: Question on Call by reference

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    283

    Question on Call by reference

    Can someone explain me in detail, why strings and classes considered call by reference.

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Question on Call by reference

    Never heard that term, but it would have to be because a string for example is an object that is found in the managed heap and the variable that points to it just maintains a reference to the location of the actual object in the heap. Therefore anytime you pass the variable to and from various locations within the program, you are always passing a reference (either by Value or by Reference).

    Clear as mud?

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

    Re: Question on Call by reference

    There is a difference between whether a type is a value type or a reference type, and whether you pass parameters to a method by value or by reference. A value type parameter can be passed by value or by reference. A reference type parameter can also be passed by value or by reference.

    Note also that strings are classes: instances of the String class.

    To extend a little what FourBlades posted, .NET variables are stored on the stack while reference type objects are stored on the heap. If a variable is a value type, like Integer, then the value itself is stored in the variable. If a variable is a reference type then it contains the memory address of an object on the heap, i.e. a reference to that object. If the variable does not refer to an object then the stack variable contains all zeroes, i.e. null.

    Passing parameters by value or by reference is different. When you pass a parameter to a method you are passing a variable. If you pass the variable by value then you are passing a copy of that variable. If you pass the variable by reference then you're passing a reference to that variable. So, here are the four possible combinations:

    1. Pass a copy of a value.
    2. Pass a copy of a reference.
    3. Pass a reference to a value.
    4. Pass a reference to a reference.

    Now if you pass a copy of a value, any changes you make inside the method will not affect the original value because they are unrelated.

    If you pass a reference to a value then any changes you make inside the method will also affect the original variable, because the method parameter refers to that variable.

    If you pass a copy of a reference then any changes you make to the object will also affect the original, because both the original and the copy refer to the same object, i.e. they are two references to the same object. If you assign a new object to the parameter inside the method that will NOT affect the original variable.

    If you pass a reference to a reference then any changes you make to the object will stick AND if you assign a new object to the parameter that will also stick.
    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