Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ByValue parameter is not used

ByValue parameter is not used

Scheduled Pinned Locked Moved C#
data-structuresquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seraphin
    wrote on last edited by
    #1

    Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it? Thanks in advance for every tip. private void button4_Click(object sender, System.EventArgs e) { CheckArray(originalArray); } private void CheckArray(int[,] iArray) { int[,] copyArray = iArray; copyArray[0,0] = 0; }

    L 1 Reply Last reply
    0
    • S Seraphin

      Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it? Thanks in advance for every tip. private void button4_Click(object sender, System.EventArgs e) { CheckArray(originalArray); } private void CheckArray(int[,] iArray) { int[,] copyArray = iArray; copyArray[0,0] = 0; }

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

      Seraphin wrote:

      Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it?

      Yes. But...

      private void button4_Click(object sender, System.EventArgs e)
      {
      CheckArray(originalArray);
      }

      private void CheckArray(int[,] iArray)
      {
      int[,] copyArray = iArray;
      copyArray[0,0] = 0;
      }

      You've not copied the array, you've only created another reference to the same array. When you assign a value to the array using the copied reference, the original array is affected, too. iArray and copyArray both reference the same array. Remember, iArray is just a reference to an array held somewhere in memory. References are just variables. The reference is passed by value so that if you did something like this:

      private void button4_Click(object sender, System.EventArgs e)
      {
      CheckArray(originalArray);
      }

      private void CheckArray(int[,] iArray)
      {
      iArray = new int[10, 10];
      iArray[0, 0] = 0;
      }

      The originalArray will not be affected. You've only changed the iArray variable, which has local scope within the method. To copy the array, you have to create a new array, not just a new reference, and copy the contents of the old array into the new array. Unfortunately, the CopyTo method only works for one-dimensional arrays, so you may have to write code that does the copying by hand. Or maybe someone else knows of a better way.

      S 1 Reply Last reply
      0
      • L Leslie Sanford

        Seraphin wrote:

        Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it?

        Yes. But...

        private void button4_Click(object sender, System.EventArgs e)
        {
        CheckArray(originalArray);
        }

        private void CheckArray(int[,] iArray)
        {
        int[,] copyArray = iArray;
        copyArray[0,0] = 0;
        }

        You've not copied the array, you've only created another reference to the same array. When you assign a value to the array using the copied reference, the original array is affected, too. iArray and copyArray both reference the same array. Remember, iArray is just a reference to an array held somewhere in memory. References are just variables. The reference is passed by value so that if you did something like this:

        private void button4_Click(object sender, System.EventArgs e)
        {
        CheckArray(originalArray);
        }

        private void CheckArray(int[,] iArray)
        {
        iArray = new int[10, 10];
        iArray[0, 0] = 0;
        }

        The originalArray will not be affected. You've only changed the iArray variable, which has local scope within the method. To copy the array, you have to create a new array, not just a new reference, and copy the contents of the old array into the new array. Unfortunately, the CopyTo method only works for one-dimensional arrays, so you may have to write code that does the copying by hand. Or maybe someone else knows of a better way.

        S Offline
        S Offline
        Seraphin
        wrote on last edited by
        #3

        Thx, but what "ref" stands for, if you use it in a parameter list? If its just a reference, why I should use "ref"? I've read that ByValue is the standard, so I am confused ...

        L 1 Reply Last reply
        0
        • S Seraphin

          Thx, but what "ref" stands for, if you use it in a parameter list? If its just a reference, why I should use "ref"? I've read that ByValue is the standard, so I am confused ...

          L Offline
          L Offline
          Leslie Sanford
          wrote on last edited by
          #4

          Hmm, I thought I had replied to this post, but it's not showing up. Here is the second attempt (good thing I had my answer saved). Say I did this:

          private void button4_Click(object sender, System.EventArgs e)
          {
          CheckArray(ref originalArray);
          }

          private void CheckArray(ref int[,] iArray)
          {
          iArray = new int[10, 10];
          iArray[0, 0] = 0;
          }

          This will actually change the originalArray variable. It now points to the new array I created in the CheckArray method. Without the ref modifier, it would only change the local iArray variable. A reference is a variable that points to some object in memory. The originalArray variable is a reference to an array held in memory somewhere. It allows me to perform operations on the array. However, operations on the originalArray variable itself only effect that variable. For example:

          int[] arrayA = new int[10];
          int[] arrayB = arrayA;

          // Both arrayA and arrayB reference the same array.

          // An operation on the array via the arrayA reference variable.
          // Does not affect arrayB.
          arrayA[0] = 42;

          // An operation on the arrayA reference variable itself.
          arrayA = null;

          // This will print 42 because the arrayB reference variable
          // still points to the array.
          Console.WriteLine(arrayB[0]);

          When a reference variable is passed by value to a method, a copy of the reference variable itself is made, much like what I did above with the arrayB variable. You'll notice that when I nulled out the arrayA, it did not affect the arrayB variable. It's the same when you pass a reference by value to a method. operations on the reference variable itself only affect the local variable. Now, the ref keyword gives me a reference to a reference, so to speak. If I modify a parameter with ref, and perform operations that change the variable itself, it also affects the reference variable that was originally passed to the method. This takes awhile to wrap your mind around it, but keep at it and it will eventually make sense. :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups