c# - Cannot implement type with a collection initializer because it does not implement 'System.Collections.IEnumerable' -


i'm working in c# , xna.

i have class:

class quad     {         public texture2d texture;         public vertexpositiontexture[] vertices = new vertexpositiontexture[4];     } 

and i'm trying create new instance of said class:

quad tempquad = new quad()  {     texture = quadtexture,     vertices[0].position = new vector3(0, 100, 0),     vertices[0].color = color.red }; 

which added list of "quad"s

quadlist.add(tempquad); 

i keep either getting error:

"cannot implement type collection initializer because not implement 'system.collections.ienumerable'"

or told

vertices not exist in current context.

is there reason can't create class this? being dumb? have this?:

quad tempquad = new quad();  tempquad.vertices[0].position = new vector3(0, 100, 0); tempquad.color = color.red;  quadlist.add(tempquad); 

is there way around @ all? appreciated.

the object initialize syntax expecting assignment properties on object you're initializing, attempting assign vertices[0] you're trying assign properties of index of property on object you're initializing(!).

you can use object initialization syntax long assign vertices directly:

quad tempquad = new quad()  {     texture = quadtexture,     vertices = new vertexpositiontexture[]                 {                     new vertexpositiontexture                      {                         position = new vector3(0, 100, 0),                         color = color.red                     },                      // ... define other vertices here                 } }; 

as can see, gets pretty messy pretty quickly, you'd better off initializing array outside of object initialization:

var vertices = new vertexpositiontexture[]                 {                     new vertexpositiontexture                      {                         position = new vector3(0, 100, 0),                         color = color.red                     },                      // ... define other vertices here                 };  quad tempquad = new quad()  {     texture = quadtexture,     vertices = vertices }; 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -