I'm having about 4 int arrays that need their length calculated, assigned to them and then populated. I was trying to thin out the code by using a function with parameters, instead of repeating long calculations 4 times, but I can't seem to set the length by designating the array as a parameter. I tried something like the code below:
for(int i=0; i<4; i++)
if(i==0) SetLength(array1);
else if(i==1) SetLength(array2);
else if(i==2) SetLength(array3);
else if(i==3) SetLength(array4);
SetLength(int[] array)
{
//calculations for length here
//int result=...;
array = new int[result];
//getting info for populating the array
for(int i=0; i<result; i++)
array[i]=some_value[i];
}
Most of the code seems to work, except for the length assigning part. Any ideas?
array = new int[result];Why you initializing array again in method?.AddmethodsSetLength(array1); SetLength(array2); ...(with the fixes suggested by the answers).