0

I am trying to initialize a newly created empty array by using a method. Example below. This is the structure of the code that I want to create.

var v = exClass[5,5];
v.ExtensionMethodThatWillInitilize();

What I want ExtensionMethodThatWIllInitilize to do is following:

for(int y = 0 ; y < exClass.getLength(0); y++ ) {
for(int x = 0 ; x < exClass.getLength(1); x++ ) {
v[y,x] = new instanceObject();
}}

So I came up with below code...

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        var oldEx = new ExClass[10, 10];
        var newEx = oldEx.init();
    }
}
public class ExClass
{
    public string exString
    public ExClass() {
    exString = " I AM NEW! YES! ";
 }
}
public static class tools
{
    static public ExClass[,] init(this ExClass[,] start)
    {
        var newArray = new ExClass[start.GetLength(0), start.GetLength(1)];
        for (int y = 0; y < start.GetLength(0); y++)
        {
            for (int x = 0; x < start.GetLength(1); x++)
            {
              newArray[y, x] = new ExClass();
            }
        }
        return newArray;
    }
}

Yet the problem is that I do not know how to modify my init method so that it can take any type of array and return corresponding array that had been initialize. I tried to use generic type T but my lack of experience seems to fail.

Summery:

1) how do you come up with a method that will initialize an empty array in a single step.

2) how do you make sure that the method can initialize any type of array(assuming that the element in the array has a constructor that does not take any argument)

2
  • use a dynamic array {} or {,} Commented Feb 23, 2012 at 18:20
  • Try posting your attempt at a generic method, because that most certainly is the solution to this. I'm pretty sure where you went wrong, but it's better to not guess. Commented Feb 23, 2012 at 18:21

2 Answers 2

1

Generics do work. You need to declare your method as a generic type.

I have modified the above example as follows:

class Program
{
    static void Main(string[] args)
    {
        var oldEx = new ExClass[10, 10];
        var newEx = oldEx.init<ExClass>();
    }
}
public class ExClass
{
    public string exString = "I AM NEW";
}
public static class tools
{
    static public T[,] init<T>(this T[,] start)
    {
        var newArray = new T[start.GetLength(0), start.GetLength(1)];
        for (int y = 0; y < start.GetLength(0); y++)
        {
            for (int x = 0; x < start.GetLength(1); x++)
            {
                newArray[y, x] = Activator.CreateInstance<T>();
            }
        }
        return newArray;
    }
}

As an answer to your comment below:

class Program
{
    static void Main(string[] args)
    {
        var oldEx = tools.init<ExClass>(10, 10);

    }
}
public class ExClass
{
    public string exString = "I AM NEW";
}
public static class tools
{
    static public T[,] init<T>(int x,int y)
    {
        var newArray = new T[x, y];
        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                newArray[i, j] = Activator.CreateInstance<T>();
            }
        }
        return newArray;
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Would it be possible to have the init method to directly modify original 'start'?
so that I do not need to reassign the whole thing over again?
+1 vote, I will try to post more details that I wana know but I gotta go to class soon! thank you so much for this modification!
Yes it is possible, but you need to pass both the arguments as parameter. I have edited my answer above and added the code for your requirement.
Why use reflection when you can just constrain T to have a default constructor?
|
1

Check out this generic method, which uses the new generic constraint:

 static public void Init<T>(this T[,] array) where T : new()
 {
    for (int y = 0; y < array.GetLength(0); y++)
    {
       for (int x = 0; x < array.GetLength(1); x++)
       {
          array[y, x] = new T();
       }
    }
  }

The main differences from your code are:

1) The method is Init to specify a generic parameter, and there where T : new() constraint ensures that we can call new T();

2) Instead of returning a new, initialized array, I'm simply initializing the array in-place. Seems more logical. If you want, you can use your original version, but I don't understand why you'd need to.

1 Comment

Thank you so much for this version of code. I am eternally happy to initialize my array with this method! your code will live in my extensions class FOREVER!

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.