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.WriteLi ne("static constr."); }
public MyPointC()
{ Console.WriteLi ne("0 args constr."); x=1; y=2;}
public MyPointC(int t)
{ Console.WriteLi ne("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 1 7099
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.goo gle.com... 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.WriteLi ne("static constr."); }
public MyPointC() { Console.WriteLi ne("0 args constr."); x=1; y=2;}
public MyPointC(int t) { Console.WriteLi ne("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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 of the ones I
looked, classified by how I would rewrite them (if I could):
* Rewritable as def statements (<name> = lambda <args>: <expr> usage)
These are lambdas used when a lambda wasn't needed -- an anonymous
function was created with...
|
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 a single argument? I'm
sorry, this explanation is just atrocious, but I can't think of exactly how
to word it. Maybe an example...
Take for instance Function.apply. It takes 1-2 arguments, the first being
the object to use as the context, and...
|
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. The elements of the array are constructed using the default
> constructor for the instantiating type T.
Does that mean that, on built-in types (e.g. int), "default
initialization" (as described in 8.5) is performed?
|
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
instantiate an array of structures; consider the following useless
code :
using System;
struct MyPointS
|
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 writing the JavaScript doesn't have to
pass the index
in the "onChange" event name.
I thought that one might be able to use "this.value" or compare this as
| |
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 code below which when
executed generates the following error message: 'There is already an
open datareader with this command which must be closed first'
Private Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As
|
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_){}
};
|
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 <class T>
TestClass()
|
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):
//------------------------------------------------------------------
template<typename Data_t>
class SmartPointer
{
Data_t* data;
void(*deleterFunc)(Data_t*);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |