473,396 Members | 1,755 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.

constructor

Following texts are from C# spec.

The optional constructor-initializer specifies another instance
constructor to invoke before executing the statements given in the
constructor-body of this instance constructor. This is described
further in Section 10.10.1.
So this means:

public class c1
{
(1)public c1(int i) : this(i, i)
{
}

(2)public c1(int i, int j)
{
}
}

constructor (2) will be called before constructor (1).

It is OK for simple parameters. What if parameter j can only get from
a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).
How can we do this in C#?

Thanks
linkspeed
Nov 15 '05 #1
10 2217

"linkspeed" <li***********@yahoo.com> wrote in message
news:f9*************************@posting.google.co m...
Following texts are from C# spec.

The optional constructor-initializer specifies another instance
constructor to invoke before executing the statements given in the
constructor-body of this instance constructor. This is described
further in Section 10.10.1.
So this means:

public class c1
{
(1)public c1(int i) : this(i, i)
{
}

(2)public c1(int i, int j)
{
}
}

constructor (2) will be called before constructor (1).

It is OK for simple parameters. What if parameter j can only get from
a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).
How can we do this in C#?

This is an annoying limitation. You are pretty much required to use a
private static member to perform the calculation and return the proper value
for the parameter as there is no way to call this() or base() from within a
constructor.
Thanks
linkspeed

Nov 15 '05 #2
linkspeed.... hmm. would a class factory work here

http://www.geocities.com/jeff_louie/OOP/oop4.htm

Regards,
Jeff
It is OK for simple parameters. What if parameter j can only get from

a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
What you shoudl do then is have a .Start or .Init member that must be called
before anything works. Until thats called, throw an exception to enforce it.
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:en**************@tk2msftngp13.phx.gbl...

"linkspeed" <li***********@yahoo.com> wrote in message
news:f9*************************@posting.google.co m...
Following texts are from C# spec.

The optional constructor-initializer specifies another instance
constructor to invoke before executing the statements given in the
constructor-body of this instance constructor. This is described
further in Section 10.10.1.
So this means:

public class c1
{
(1)public c1(int i) : this(i, i)
{
}

(2)public c1(int i, int j)
{
}
}

constructor (2) will be called before constructor (1).

It is OK for simple parameters. What if parameter j can only get from
a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).
How can we do this in C#?

This is an annoying limitation. You are pretty much required to use a
private static member to perform the calculation and return the proper

value for the parameter as there is no way to call this() or base() from within a constructor.
Thanks
linkspeed


Nov 15 '05 #4
That is because "this" is NOT a valid reference until the ctor has exited.

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:en**************@tk2msftngp13.phx.gbl...

"linkspeed" <li***********@yahoo.com> wrote in message
news:f9*************************@posting.google.co m...
Following texts are from C# spec.

The optional constructor-initializer specifies another instance
constructor to invoke before executing the statements given in the
constructor-body of this instance constructor. This is described
further in Section 10.10.1.
So this means:

public class c1
{
(1)public c1(int i) : this(i, i)
{
}

(2)public c1(int i, int j)
{
}
}

constructor (2) will be called before constructor (1).

It is OK for simple parameters. What if parameter j can only get from
a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).
How can we do this in C#?

This is an annoying limitation. You are pretty much required to use a
private static member to perform the calculation and return the proper

value for the parameter as there is no way to call this() or base() from within a constructor.
Thanks
linkspeed


Nov 15 '05 #5

<di********@discussion.microsoft.com> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
That is because "this" is NOT a valid reference until the ctor has exited.
You should actually look into that. "this" can be used in a constructor(and
is implicitly used *quite* often, you shouldn't use it with regard to
virtual methods, but that is another issue all together. The issue is
because the C# team decided to not allow constructor calls from within a
constructor. It has to be done before a constructor body has been
executed(this is done for reasons of simplicitly, you should usually call
the base constructor before touching any instance members, the C# syntax
forces this, others do not).

In reply to your other post, that isn't a pattern I'd recommend in most
cases. If you expect the end user to call it, you have a huge problem: you
are forcing what effictivly is two constructors on the user. If you expect
inheriting code to call it you leave yourself with a method that can royally
screw your class if called by a method outside of the constructor, or result
in stupid bugs if its missed(or what if its called twice? The what ifs get
to be too complex). In most cases this is by far the worst path.

I would almost always recommend a factory, which would be a static method or
instance class(usually referenced statically) that generates your object,
performing the calculations instead of relying on constructors or by using a
private static method to perform the work of parameter calcuation over using
a generic Init method.

Now, I've said in most cases a number of times. There are situations where
an object should have an initalizing method, but usually that method should
be used in a loading sense. Such an example can be seen on XmlDocument,
where the document is capable of loading an Xml node *after* its creation.
Ideally this method should be designed to allow your class to start from
scratch on new data and should probably only be done when the end user will
have a simplier time because of it. Other reasons may be to avoid
constructor requirements when using an interface based system, or what
amounts to the interface itself needs to be initalized with specific data.
This can often be circumvented with an appropriate factory, however.

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:en**************@tk2msftngp13.phx.gbl...

"linkspeed" <li***********@yahoo.com> wrote in message
news:f9*************************@posting.google.co m...
Following texts are from C# spec.

The optional constructor-initializer specifies another instance
constructor to invoke before executing the statements given in the
constructor-body of this instance constructor. This is described
further in Section 10.10.1.
So this means:

public class c1
{
(1)public c1(int i) : this(i, i)
{
}

(2)public c1(int i, int j)
{
}
}

constructor (2) will be called before constructor (1).

It is OK for simple parameters. What if parameter j can only get from
a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).
How can we do this in C#?

This is an annoying limitation. You are pretty much required to use a
private static member to perform the calculation and return the proper

value
for the parameter as there is no way to call this() or base() from

within a
constructor.
Thanks
linkspeed



Nov 15 '05 #6

It might be worth explaining this a little further Linkspeed. Do you mean that when you create your class, you need to perform a calculation on a parameter that is then stored within the class? If you cant call one constructor over another because of this, then I would suspect maybe the approach needs revising. It is right the object should do the calculation, so why not pass in the parameters and let the object perform the calculation? In your situation a client of the class could invoke the second constructor - how do you know the vlaue being passed in is correct? Who calculated it? Why didnt this object calculate it?

----- linkspeed wrote: -----

Following texts are from C# spec.

The optional constructor-initializer specifies another instance
constructor to invoke before executing the statements given in the
constructor-body of this instance constructor. This is described
further in Section 10.10.1.
So this means:

public class c1
{
(1)public c1(int i) : this(i, i)
{
}

(2)public c1(int i, int j)
{
}
}

constructor (2) will be called before constructor (1).

It is OK for simple parameters. What if parameter j can only get from
a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).
How can we do this in C#?

Thanks
linkspeed

Nov 15 '05 #7
You won't be able to use that syntax but you can do the following

c1(int i) {
int j;
// process j;
this(i, j);
}

I think I have seen this work before, but let me know if this violates
something.

"linkspeed" <li***********@yahoo.com> wrote in message
news:f9*************************@posting.google.co m...
Following texts are from C# spec.

The optional constructor-initializer specifies another instance
constructor to invoke before executing the statements given in the
constructor-body of this instance constructor. This is described
further in Section 10.10.1.
So this means:

public class c1
{
(1)public c1(int i) : this(i, i)
{
}

(2)public c1(int i, int j)
{
}
}

constructor (2) will be called before constructor (1).

It is OK for simple parameters. What if parameter j can only get from
a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).
How can we do this in C#?

Thanks
linkspeed

Nov 15 '05 #8
Nick that wont compile.

"Nick" <ne**@zigamorph.com> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
You won't be able to use that syntax but you can do the following

c1(int i) {
int j;
// process j;
this(i, j);
}

I think I have seen this work before, but let me know if this violates
something.

"linkspeed" <li***********@yahoo.com> wrote in message
news:f9*************************@posting.google.co m...
Following texts are from C# spec.

The optional constructor-initializer specifies another instance
constructor to invoke before executing the statements given in the
constructor-body of this instance constructor. This is described
further in Section 10.10.1.
So this means:

public class c1
{
(1)public c1(int i) : this(i, i)
{
}

(2)public c1(int i, int j)
{
}
}

constructor (2) will be called before constructor (1).

It is OK for simple parameters. What if parameter j can only get from
a long and complex calculation? This requires the body of constructor
(1) is called before constructor (2).
How can we do this in C#?

Thanks
linkspeed


Nov 15 '05 #9
Thanks for all posts.

There are some workarounds for that problem,
but a futher question is:

from language design point, does this kind of contructor
calling style in C# have any advantage?
Nov 15 '05 #10
Its purpose is to ensure the order of constructor calls. By forcing this
syntax it is possible to ensure that a base constructor will always be
called before a constructor accesses an instance member. It was a choice
made based on common usage I imagine. It *usually* isn't a problem. Often
times when it causes a problem you need to re-examine your design(I've run
into it a few times as well).

"linkspeed" <li***********@yahoo.com> wrote in message
news:f9**************************@posting.google.c om...
Thanks for all posts.

There are some workarounds for that problem,
but a futher question is:

from language design point, does this kind of contructor
calling style in C# have any advantage?

Nov 15 '05 #11

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

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.