1

I understand why I should use ref when writing a function to swap two values, but I don't know how to use the keyword on an entire array. It sounds silly, but I have tried sticking the keyword everywhere I could possibly think of (e.g. before the parameter, before the variables, etc...) but I still get the following error:

Error 1 An object reference is required for the non-static field, method, or property 'Swap.Program.swapRotations(int[])'

Here is what I have done so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Swap
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] A = {0, 1, 2, 3, 4, 5, 6, 7};

            swapRotations(A);

            for (int i = 0; i < A.Length; i++)
                Console.WriteLine(A[i]);

            Console.WriteLine("\nPress any key ...");
            Console.ReadKey();
        }

        private void swapRotations(int[] intArray)
        {
            int bone1Rot = intArray[3];
            int bone2Rot = intArray[5];

            // Make the swap.
            int temp = bone1Rot;
            bone1Rot = bone2Rot;
            bone2Rot = temp;
        }
    }
}
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
  • 4
    to fix compilation error: make `swapRotations` static too, because you call it from static method `Main`; and i don't understand what you are trying to do with `ref` – ASh Nov 20 '15 at 12:00
  • 5
    This has *nothing* to do with the `ref` keyword. A static method can't call a non-static method directly, that's all. – David Nov 20 '15 at 12:02
  • 2
    If you search your error message on Google, I believe you would found your answer without asking here. – Soner Gönül Nov 20 '15 at 12:03
  • 1
    Possible duplicate of [An object reference is required for the nonstatic field, method, or property on a Windows form](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-on) – TheLethalCoder Nov 20 '15 at 12:04

3 Answers3

9

Simple just change:

private void swapRotations(int[] intArray);

to:

private static void swapRotations(int[] intArray);

The problem is because the calling method is static so any method it uses either need to have an object referenced to them or be static themselves.

Also have a look at @ASh's answer on how to do the swapRotations function "properly". Note I say properly because there could still be an IndexOutOfRange exception thrown. To do this properly and genericly I'd do something along the lines of the following:

private static void SwapIndexes(int[] array, int index1, int index2)
{
    if (index1 >= array.Length || index2 >= array.Length)
        throw new Exception("At least one of the indexes is out of range of the array");

    int nTemp = array[index1];
    array[index1] = array[index2];
    array[index2] = nTemp;
}
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
  • if you want to do it *properly* indeed, do not forget `index1<0` and `index2<0` cases; and *genericly* would be something along the lines: `void SwapIndexes(T[] array, int index1, int index2) { T nTemp = array[index1]; array[index1] = array[index2]; array[index2] = nTemp; }` – ASh Nov 22 '15 at 15:49
  • @ASh I wasn't critising your method just showing **some** safety checks that can take place, anymore can be developed by the OP – TheLethalCoder Nov 23 '15 at 09:06
2

swap method doesn't work, because you don't change array at all

no need in ref, just set array elements

private void swapRotations(int[] intArray)
{
    int temp = intArray[3];
    intArray[3] = intArray[5];
    intArray[5] = temp;
}
ASh
  • 34,632
  • 9
  • 60
  • 82
-1

Arrays are Reference types. So there is no need to use ref keyword to pass an array. And your problem is not in ref keyword, but you must call only static methods from static method. For example:

    static void Main(string[] args)
    {
        int[] A = {0, 1, 2, 3, 4, 5, 6, 7};

        swapRotations(A);

        for (int i = 0; i < A.Length; i++)
            Console.WriteLine(A[i]);

        Console.WriteLine("\nPress any key ...");
        Console.ReadKey();
    }

    private static void swapRotations(int[] intArray)
    {           
        // Make the swap.
        int temp = intArray[3];  
        intArray[3] = intArray[5];
        intArray[5] = temp;
    }
StepUp
  • 36,391
  • 15
  • 88
  • 148