Hi people I found I can only reset the values in the array but I can resize it in a new method and return it back to main method,

static void Main()
        {
            int[] myArray = new int[4];
            setLength(myArray, 8);
        }

        static void setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

This is the code what I wrote, it works at the end of the setLength method,
but after it turns back to the main mathod, the array returns to the original one...

Could someone help me please?

Recommended Answers

All 5 Replies

You didnt set the return type to the setLenght method. You have to change the code to:

static void Main()
        {
            int[] myArray = new int[4];
            myArray = setLength(myArray, 8);
        }

        static int[] setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
            return myArray;
        }

or you can use ref keyword, to return, but I would suggest you to use upper example:

static void Main()
        {
            int[] myArray = new int[4];
            setLength(ref myArray, 8);
        }

        static void setLength(ref int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

You didnt set the return type to the setLenght method. You have to change the code to:

static void Main()
        {
            int[] myArray = new int[4];
            myArray = setLength(myArray, 8);
        }

        static int[] setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

or you can use ref keyword, to return, but I would suggest you to use upper example:

static void Main()
        {
            int[] myArray = new int[4];
            setLength(ref myArray, 8);
        }

        static void setLength(ref int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

Thanks so much man, the second solution works
lol I forgot "ref"...

Actually I tried your first solution before, but it didn't work,
the array still return to the original one after it goes back to main method...

anyway, thanks so much

UPS, so sorry.. I guess Im already asleeping here.
I forgot a return keyword:

static void Main()
        {
            int[] myArray = new int[4];
            myArray = setLength(myArray, 8);
        }

        static int[] setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
            return myArray;
        }

This will do the trick mate.
good night.

UPS, so sorry.. I guess Im already asleeping here.
I forgot a return keyword:

static void Main()
        {
            int[] myArray = new int[4];
            myArray = setLength(myArray, 8);
        }

        static int[] setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
            return myArray;
        }

This will do the trick mate.
good night.

Yea this is what I tried before,
you can try to run it and it really don't work...
anyway, thanks so much for help and good night.

Sure it does. I tried it.
Take a look at this picture, its a leaving prove:
http://file.si/files/46441_rioid/C%23_ResizeArray.jpg

Picture was taken after the code returned from the method which resized it. As you can see there are 8 indexes in the array. So this means resizing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.