473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Which constructor is used when instantiating an array of objs ?

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
Nov 16 '05 #1
1 7078
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" <fa*********************@yahoo.it> wrote in message
news:23**************************@posting.google.c om...
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

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
9
by: Pierre Senellart | last post by:
The C++ standard states (26.3.2.1), about std::valarray constructors: > explicit valarray(size_t); > > The array created by this constructor has a length equal to the value of > the argument....
2
by: david | last post by:
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...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
9
by: toton | last post by:
I have a class like template<typename T> class my_class{ public: int x_; public: template<typename U> my_class(const my_class<U>& other ) : x_(other.x_){} };
3
by: Lawrence Spector | last post by:
How does one call a templated constructor inside of a class when instantiating an object? I made up a quick sample to demonstrate. #include <iostream> class TestClass { public: template...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.