Original Post
Hey,
Being not totally advanced yet with C# I am trying to develop a function that would instantiate a list of object.
[source lang="java"]public static List CreateObjLeft(T type) {
List temp = new List();
for (int i = 0; i < 10; i++) {
temp.Add(type); }
return temp;
}[/source]
This is somehow working, I get my 10 objects but I need to access each instance. I created a default constructor for the object, but at the moment all objects in the list have the same attributes and I need to access the position (it is for a game) so that they do not all show on top of each other.
I am trying to create a pattern of object (circle, triangle...) and I would like to be able to use the same generic function for whatever object I want to create (pretty mush the principle of generic methods...).
I have also tried to cast the object inside as such:
[source lang="csharp"]if (type is Asteroid) {
((Asteroid)type).position = new Vector2(0,0);
}[/source]
But then I get an error Cannot convert type T to my own class. Or trying to access the object of the list in the same fashion:
[source lang="csharp"]if (temp is Asteroid) {
((Asteroid)temp)[0].position = new Vector2(0,0);
}[/source]
Error 5 Cannot convert type 'System.Collections.Generic.List' to 'Tutorial.Asteroid'
Does anyone has a solution in mind? Or I thought of trying to add my custom object type to Generic.List but could not really get it...
Thanks
Being not totally advanced yet with C# I am trying to develop a function that would instantiate a list of object.
[source lang="java"]public static List
List
for (int i = 0; i < 10; i++) {
temp.Add(type); }
return temp;
}[/source]
This is somehow working, I get my 10 objects but I need to access each instance. I created a default constructor for the object, but at the moment all objects in the list have the same attributes and I need to access the position (it is for a game) so that they do not all show on top of each other.
I am trying to create a pattern of object (circle, triangle...) and I would like to be able to use the same generic function for whatever object I want to create (pretty mush the principle of generic methods...).
I have also tried to cast the object inside as such:
[source lang="csharp"]if (type is Asteroid) {
((Asteroid)type).position = new Vector2(0,0);
}[/source]
But then I get an error Cannot convert type T to my own class. Or trying to access the object of the list in the same fashion:
[source lang="csharp"]if (temp is Asteroid) {
((Asteroid)temp)[0].position = new Vector2(0,0);
}[/source]
Error 5 Cannot convert type 'System.Collections.Generic.List
Does anyone has a solution in mind? Or I thought of trying to add my custom object type to Generic.List
Thanks