I'm making a method that takes 2 arrays and adds them together. (I'm a newbie and this is a school assignment, and I have learned that some people don't like to see school questions here. But I have done alot of the work already, I'm just stuck now.)
public static int[] Concat(int[] array1, int[] array2) //array1 = 1, 2, 3, 4, 5 array2 = 6, 7, 8, 9, 10.
{
int i = array1.Length + 1;
int[] array3 = new int[array1.Length + array2.Length];
Class1.CopyTo(array1, array3, 0);
Class1.CopyTo(array2, array3, i);
Class1.PrettyPrint(array3);
return array3;
}
In this method im referring to 2 other methods I did previously:
public static int[] CopyTo(int[] arr1, int[] arr2, int start)
{
for(int i = 0; i < arr1.GetLength(0); i++)
{
if (start <= i)
{
arr2[i] = arr1[i];
}
}
return null;
}
This one is for copying indexes of an array into another array. The start variable is the index where I will first start to copy.
public static int[] PrettyPrint(int[] intArray)
{
string result = string.Join(", ", intArray);
Console.WriteLine(result);
return null;
}
And this prints out an array in a string.
The problem lies in the Concat method. It works for the first copy. Where I copy array1 into array3.
Class1.CopyTo(array1, array3, 0);
But for the second array, it adds nothing and all I get from typing it out as a string is:
1, 2, 3, 4, 5, 0, 0, 0, 0, 0.
I don't understand why only the first copy works.
Also a follow up question.
In my main program tab I could no longer reference these methods with an instance of my class Class1 lab2 = new Class1();
. But I had to call them by using Class1 instead of lab2 (which worked for my other methods).
To be able to call a method inside another method in the same class I had to add "static" and by adding "static" I had to change how I call the method in my main program tab.
I didn't really understand why I had to first add "static" and then change how I call the method. Why did I have to change the 2 methods that were called to static? And why couldn't I call those changed methods with the instance lab2?
Sorry for the long question!