0

I have a problem with the code:

namespace hello
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int xx = 5;
            string[,] myArray = new string[1, 5];
            if (xx > 4)
            {
                ResizeArray(ref myArray, 4, 5);
            }
            else
            {
                ResizeArray(ref myArray, 2, 5);
            }
        }
         void ResizeArray(ref string[,] original, int rows, int cols)
        {
            string[,] newArray = new string[rows, cols];
            Array.Copy(original, newArray, original.Length);
            original = newArray;
        }
    }
}

I get the error message:

An object reference is required for the non-static field, method, or property 'hello.Program.ResizeArray(ref string[,], int, int)'

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
SharpUser
  • 97
  • 8
  • 1
    Possible duplicate of [C# error: "An object reference is required for the non-static field, method, or property"](https://stackoverflow.com/questions/10264308/c-sharp-error-an-object-reference-is-required-for-the-non-static-field-method) – tkausl Jun 23 '19 at 10:06
  • 2
    You need to make `ResizeArray` static, since you call it from a static context (your `Main` method). – Tobias Tengler Jun 23 '19 at 10:06

1 Answers1

1

Static members can't access non-static member without creating an instance. You just need:

static void ResizeArray(ref string[,] original, int rows, int cols)
Saeid Amini
  • 1,313
  • 5
  • 16
  • 26