2

I need to create 2 methods. Method 1 (CreateArray) shall allow the user to enter the length of an array they would like to create and then enter the value for each element. Method 2 (PrintArray) prints the array created in Method 1.

Both methods work fine on a standalone basis. However, I don't know how to pass CreateArray method to PrintArray method.

class Calculation
{
    public int[] CreateArray (int a)
    {
        int count = 1;
        int[] CreateArray = new int[a];
        for (int i = 0; i < CreateArray.Length; i++)
        {
            Console.Write($"{count}.) Enter a value - ");
            int userInput = int.Parse(Console.ReadLine());
            CreateArray[i] = userInput;
            count++;
        }
        return CreateArray;
    }

    public void PrintArray(int[] numbers)
    {
        int elementNum = 1;
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.WriteLine($"{elementNum}.) {numbers[i]}");
            elementNum++;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.Write("Enter the Length of an array you would like to create: ");
        int arrayLength = int.Parse(Console.ReadLine());

        Calculation calc = new Calculation();
        calc.CreateArray(arrayLength);

    }
}
1
  • Why do you want to pass the CreateArray method to the PrintArray method? Maybe you mean you want to pass the array which is created to the PrintArray method? Commented Jun 13, 2017 at 10:25

1 Answer 1

1

You need to store the results of "calc.CreateArray(arrayLength);" in a local variable and pass it to PrintArray():

int[] newArray = calc.CreateArray(arrayLength);
calc.PrintArray(newArray);
Sign up to request clarification or add additional context in comments.

5 Comments

Side Note: You should probably either change your Calculation class to store the passed values as a proberty inside that instance or make CreateArray and PrintArray static and call them directly via "Calculation.CreateArray(len)" and "Calculation.PrintArray(arr)"
SebRut: And every time you want a new array you have create an instance of Calculation instead of an int[]. Guess that's not a good idea. And we haven't started talking about testability .
@MightyBadaboom that's why I also suggested to make the methods static, which imo is the right way to do this kind auf task.
@SebBut that's why I mentioned testability. You can't mock static methods as far as I know for example. And if you want more then one array you still have to instantiate x instances of the calculation class instead of x arrays if you store the variables in the Claculation class. That's why I would solve it like you wrote in the answer and not like you wrote in your comment.
Thankyou. I am new to programming so I was just learning how to create methods and call them in the main method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.