EDN Admin
Well-known member
Hi,
Lets say I have an array:string[] a = new string[] { "a", "b", "c" };
And I have a function to make it longer:a = MakeArrayLonger(a);
private string[] MakeArrayLonger(string[] a)
{
string[] aNew = new string[a.Length + 1];
for (int i = 0; i < a.Length; i++)
aNew = a;
return aNew;
}
It works! Great!
What if I have an int array?int[] ii = new int[] { 1, 2, 3, 4 };
Id need to create another overloaded functionrivate int[] MakeArrayLonger(int[] a)
{
int[] aNew = new int[a.Length + 1];
for (int i = 0; i < a.Length; i++)
aNew = a;
return aNew;
}
But then, Id need to make many functions for each kind of array: single, double, MyClass...
Q) Is there a way to make a generic function that works on all arrays?
NB: Ive tried object[], but int[] doesnt work:a = (string[])MakeArrayLonger(a);
ms = (A1EmailMessage[])MakeArrayLonger(ms);
i = (int[])MakeArrayLonger(i); // *** compile err ***
private object[] MakeArrayLonger(object[] a)
{
object[] aNew = new object[a.Length + 1];
for (int i = 0; i < a.Length; i++)
aNew = a;
return aNew;
}
Thank you,
Andy
PS I know I could use List<string> to add elements, Im trying to find out a way of making a general function that would apply to all arrays.
Thanks, Andy
View the full article
Lets say I have an array:string[] a = new string[] { "a", "b", "c" };
And I have a function to make it longer:a = MakeArrayLonger(a);
private string[] MakeArrayLonger(string[] a)
{
string[] aNew = new string[a.Length + 1];
for (int i = 0; i < a.Length; i++)
aNew = a;
return aNew;
}
It works! Great!
What if I have an int array?int[] ii = new int[] { 1, 2, 3, 4 };
Id need to create another overloaded functionrivate int[] MakeArrayLonger(int[] a)
{
int[] aNew = new int[a.Length + 1];
for (int i = 0; i < a.Length; i++)
aNew = a;
return aNew;
}
But then, Id need to make many functions for each kind of array: single, double, MyClass...
Q) Is there a way to make a generic function that works on all arrays?
NB: Ive tried object[], but int[] doesnt work:a = (string[])MakeArrayLonger(a);
ms = (A1EmailMessage[])MakeArrayLonger(ms);
i = (int[])MakeArrayLonger(i); // *** compile err ***
private object[] MakeArrayLonger(object[] a)
{
object[] aNew = new object[a.Length + 1];
for (int i = 0; i < a.Length; i++)
aNew = a;
return aNew;
}
Thank you,
Andy
PS I know I could use List<string> to add elements, Im trying to find out a way of making a general function that would apply to all arrays.
Thanks, Andy
View the full article