D
david
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway,
although Ollie's answer makes perfectly sense when dealing with
classes, it doesn't seem to me to apply as well if you have to
instantiate an array of structures; consider the following useless
code :
using System;
struct MyPointS
{
internal int x;
internal int y;
internal static int a=-1;
static MyPointS()
{ Console.WriteLine("static constr."); }
public MyPointS(int t)
{ Console.WriteLine("1 args constr."); x=y=t; }
}
public class David
{
public static void Main()
{
int ss=MyPointS.a;
// causes static constructor to run
MyPointS s1=new MyPointS();
// causes implicit default 0 args constructor to run
MyPointS s2=new MyPointS(11);
// causes 1 args constructor to run
MyPointS[] vs=new MyPointS[5];
// no explicit constructor seems to run.
// With structures (as opposed to classes) instances
// in the array DO_GET_ALLOCATED
// (on the stack, as opposed to heap),
// so that you can do the following :
vs[3].x=vs[3].y=123;
// if MyPointS weren't a structure, but a class,
// this line would cause an exception to be raised
}
}
So the basic question is the same :
"How can I instantiate an array of objects (out of a struct),
specifying - as I do when creating single objects - which constructor
should be run, to let, for instance, the array elements (or anythinhg
else) being initialized the way I want ?"
Ollie's previous answer for classes, adapted to structures
MyPointS[] vs=new MyPointS[5];
vs[3]=new MyPointS(11);
seems to suggest that, to achieve that, you happen to instantiate
twice a given object :
When you declare the array object
MyPointS[] vs=new MyPointS[5];
// five structure objs get "physically" allocated on the stack
And when you explicitely call a costructor for a given array element
vs[3]=new MyPointS(11);
// a new vs[3] obj overwrites the one previously
// created when the array was first allocated
So, it seems "anti-economical" to me having to re-create the same
object (out of a structure definition), if you just want to have it
properly initialized.
Sorry to return again on such a basic trivial topic, but I'd like to
understand if that's a (minor) flaw of the language, or, more likely,
something basic I still can't catch.
Thanks, David
although Ollie's answer makes perfectly sense when dealing with
classes, it doesn't seem to me to apply as well if you have to
instantiate an array of structures; consider the following useless
code :
using System;
struct MyPointS
{
internal int x;
internal int y;
internal static int a=-1;
static MyPointS()
{ Console.WriteLine("static constr."); }
public MyPointS(int t)
{ Console.WriteLine("1 args constr."); x=y=t; }
}
public class David
{
public static void Main()
{
int ss=MyPointS.a;
// causes static constructor to run
MyPointS s1=new MyPointS();
// causes implicit default 0 args constructor to run
MyPointS s2=new MyPointS(11);
// causes 1 args constructor to run
MyPointS[] vs=new MyPointS[5];
// no explicit constructor seems to run.
// With structures (as opposed to classes) instances
// in the array DO_GET_ALLOCATED
// (on the stack, as opposed to heap),
// so that you can do the following :
vs[3].x=vs[3].y=123;
// if MyPointS weren't a structure, but a class,
// this line would cause an exception to be raised
}
}
So the basic question is the same :
"How can I instantiate an array of objects (out of a struct),
specifying - as I do when creating single objects - which constructor
should be run, to let, for instance, the array elements (or anythinhg
else) being initialized the way I want ?"
Ollie's previous answer for classes, adapted to structures
MyPointS[] vs=new MyPointS[5];
vs[3]=new MyPointS(11);
seems to suggest that, to achieve that, you happen to instantiate
twice a given object :
When you declare the array object
MyPointS[] vs=new MyPointS[5];
// five structure objs get "physically" allocated on the stack
And when you explicitely call a costructor for a given array element
vs[3]=new MyPointS(11);
// a new vs[3] obj overwrites the one previously
// created when the array was first allocated
So, it seems "anti-economical" to me having to re-create the same
object (out of a structure definition), if you just want to have it
properly initialized.
Sorry to return again on such a basic trivial topic, but I'd like to
understand if that's a (minor) flaw of the language, or, more likely,
something basic I still can't catch.
Thanks, David
MyPointC[] vc=new MyPointC[5];
does not allocate any instances in the array, it only cretaes the
System.Array object so therefore none of the MyPointC constructors are
called.
you would have to do:
MyPointC[] vc=new MyPointC[5];
vc[0] = new MyPointC();
vc[1] = new MyPointC(11);
vc[1] = new MyPointC(22222);
HTH
Ollie Riches
david said:I have a question (not sure if just a newbie one, or a stupid one)
whose answer I couldn't find on the C# books and tutorials I could put
my hands on.
Consider the following useless class (could be a struct as well, if
you just comment out the non static parameterless constructor) and the
Main() routine :
using System;
class MyPointC
{
int x;
int y;
internal static int a=-1;
static MyPointC()
{ Console.WriteLine("static constr."); }
public MyPointC()
{ Console.WriteLine("0 args constr."); x=1; y=2;}
public MyPointC(int t)
{ Console.WriteLine("1 args constr."); x=y=t; }
}
public class David
{
public static void Main()
{
int cc=MyPointC.a;
// causes static constructor to run
MyPointC c1=new MyPointC();
// causes 0 args constructor to run
MyPointC c2=new MyPointC(11);
// causes 1 args constructor to run
MyPointC[] vc=new MyPointC[5];
// no explicit constructor seems to run
}
}
The question is :
"How can I instantiate an array of objects (both from a class or a
struct), specifying - as I do when creating single objects - which
constructor should be run, to let, for instance, the array elements
(or anythinhg else) being initialized the way I want ?"
MyPointC[] vc=new MyPointC()[5];
or
MyPointC[] vc=new MyPointC[5]();
are both invalid syntax, rejected by the compiler.
Am I missing a basic thing or two ?!?
Thanks, David