Aaron
Aaron

Reputation: 4480

Using an array return from a function

In a function I am taking an array and resizing it. I made the function an array return type and it does what it is supposed to do. The problem I am having is figuring out how to use the returned array in my program. hiLow is a function that takes the array and changes the value, the for loop only still shows the old array. How do I get creature(the name of my array) to only display the new values?

        hiLow(creature);
        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine(creature[i].creatureInfo());
        }

Upvotes: 0

Views: 187

Answers (2)

Wesley
Wesley

Reputation: 3111

Without knowing exactly what is going on without seeing your code, one thing you will want to do is pass the array by reference into your hiLow method. This will modify the array that is being passed in and you should be able to see the new size of your array in the loop.

 hiLow(ref creature);
 for (int i = 0; i < 2; i++)
 {
     Console.WriteLine(creature[i].creatureInfo());
 }

Upvotes: 1

Matt Grande
Matt Grande

Reputation: 12157

Return crature from your method.

creature = hiLow(creature);

Upvotes: 3

Related Questions