473,466 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructor order

I am curious as to 2 things.

One is the order that the constructors are handled and the other is why
the debugger doesn't show the static objects constructor (unless you set
a breakpoint.

For example, here is my code:

************************************************** ********************
// DemonstrateDefaultConstructor - demonstrate how default
// constructors work; create a class
// with a constructor and then step
// through a few scenarios
using System;

namespace DemonstrateDefaultConstructor
{
// MyObject - create a class with a noisy constructor
// and an internal object
public class MyObject
{
// this member is a property of the class
static MyOtherObject staticObj = new MyOtherObject();
//int tom = 0;
// this member is a property of the object
MyOtherObject dynamicObj;

public MyObject()
{
Console.WriteLine("MyObject constructor starting");
//
dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}
}

// MyOtherObject - this class also has a noisy constructor
// but no internal members
public class MyOtherObject
{
public MyOtherObject()
{
Console.WriteLine("MyOtherObject constructing");
}
}

public class Class1
{
public static void Main(string[] args)
{
Console.WriteLine("Main() starting");
// create an object
MyObject localObject;
Console.WriteLine("Before localObject defined");
localObject = new MyObject();
Console.WriteLine("After localObject new statement");
// wait for user to acknowledge the results
Console.WriteLine("Press Enter to terminate...");
Console.Read();
}
}
}
************************************************** **********************

The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** ********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor). Here is the order it is handled (according to
the debugger - when you have a breakpoint on Console.Write in the
MyOtherObject constructor.

************************************************** ************
localObject
= new MyObject(); // from Main

Console.WriteLine("MyOtherObject constructing"); //MyotherObject
constructer

static MyOtherObject staticObj = new MyOtherObject(); //MyObject
definition

public MyObject() //MyObject Constructor

Console.WriteLine("MyObject constructor starting");//MyObject Constructor
************************************************** ************

Also, if you don't set the breakpoint on the MyOtherConstuctor line, it
doesn't even show up when you are tracing through the code (F11).

Thanks,

Tom.

Nov 15 '05 #1
9 2288
Thomas Scheiderich <tf*@deltanet.com> wrote:

<snip>
The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** ********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor).
Because you've got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that's what I get when I run the code.
Also, if you don't set the breakpoint on the MyOtherConstuctor line, it
doesn't even show up when you are tracing through the code (F11).


I don't know about the vagaries of the debugger, I'm afraid. Possibly
it doesn't show type initializers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi,

Just a little comment.

You would've had 'MyOtherObject constructing' before 'MyObject constructor
starting' even if the first MyOtherObject wasn't static. That is because all
initliazing code is copied at the begining of all instance constructors,
thus all code in the constructor body will follow MyOtherObject creation.
Of course how Jon said there are two 'MyOtherObject constructing' lines.

--
HTH
B\rgds
100
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Thomas Scheiderich <tf*@deltanet.com> wrote:

<snip>
The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** ********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor).


Because you've got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that's what I get when I run the code.
Also, if you don't set the breakpoint on the MyOtherConstuctor line, it
doesn't even show up when you are tracing through the code (F11).


I don't know about the vagaries of the debugger, I'm afraid. Possibly
it doesn't show type initializers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3


Jon Skeet [C# MVP] wrote:
Thomas Scheiderich <tf*@deltanet.com> wrote:

<snip>
The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************ **********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor).

Because you've got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that's what I get when I run the code.


The reason I don't have 2 "MyOtherObject constructing" messages is
because I had forgot that I had commented out the line:

// dynamicObj = new MyOtherObject();
When I uncomment the line I get the results I think you were talking about:

************************************************** *************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyOtherObject constructing
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** *************

Also, if you don't set the breakpoint on the MyOtherConstuctor line, it
doesn't even show up when you are tracing through the code (F11).


I don't know about the vagaries of the debugger, I'm afraid. Possibly
it doesn't show type initializers.

BTW, how do you tell the MyOtherObject is a class property and not just
another object. It seems to be defined as one? What differentiates it
from a normal Class definition.

Thanks,

Tom.


Nov 15 '05 #4
Hi Thomas,

It is not commented in the code you posted, though.

--
B\rgds
100

"Thomas Scheiderich" <tf*@deltanet.com> wrote in message
news:40************@deltanet.com...


Jon Skeet [C# MVP] wrote:
Thomas Scheiderich <tf*@deltanet.com> wrote:

<snip>
The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************ **********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor).

Because you've got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that's what I get when I run the code.


The reason I don't have 2 "MyOtherObject constructing" messages is
because I had forgot that I had commented out the line:

// dynamicObj = new MyOtherObject();
When I uncomment the line I get the results I think you were talking

about:
************************************************** *************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyOtherObject constructing
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** *************

Also, if you don't set the breakpoint on the MyOtherConstuctor line, it
doesn't even show up when you are tracing through the code (F11).


I don't know about the vagaries of the debugger, I'm afraid. Possibly
it doesn't show type initializers.

BTW, how do you tell the MyOtherObject is a class property and not just
another object. It seems to be defined as one? What differentiates it
from a normal Class definition.

Thanks,

Tom.

Nov 15 '05 #5
Thomas Scheiderich <tf*@deltanet.com> wrote:
BTW, how do you tell the MyOtherObject is a class property and not just
another object. It seems to be defined as one? What differentiates it
from a normal Class definition.


It's not a property at all - it's a field, and you can tell it's a
static field rather than an instance field because there's the
"static" modifier in front of the field definition.

The class *itself* has nothing special about it at all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Stoitcho Goutsev (100) [C# MVP] wrote:
Hi Thomas,

It is not commented in the code you posted, though.
Actually, it is. Maybe you are looking at something different than I am
looking at. Here is section I was looking at (which I copied from my
original post)

************************************************** *******
public MyObject()
{
Console.WriteLine("MyObject constructor starting");
// dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}

************************************************** ********

Were you looking at something else that I missed?

Thanks,

Tom.


Nov 15 '05 #7
Thomas Scheiderich <tf*@deltanet.com> wrote:
Actually, it is. Maybe you are looking at something different than I am
looking at. Here is section I was looking at (which I copied from my
original post)

************************************************** *******
public MyObject()
{
Console.WriteLine("MyObject constructor starting");
// dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}

************************************************** ********

Were you looking at something else that I missed?


No, your original post had:

public MyObject()
{
Console.WriteLine("MyObject constructor starting");
//
dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}
}

This highlights the need to be careful about formatting code when
posting it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Jon Skeet [C# MVP] wrote:
Thomas Scheiderich <tf*@deltanet.com> wrote:
Actually, it is. Maybe you are looking at something different than I am
looking at. Here is section I was looking at (which I copied from my
original post)

************************************************ *********
public MyObject()
{
Console.WriteLine("MyObject constructor starting");
// dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}

************************************************ **********

Were you looking at something else that I missed?

No, your original post had:

public MyObject()
{
Console.WriteLine("MyObject constructor starting");
//
dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}
}

This highlights the need to be careful about formatting code when
posting it.

That's interesting.

Obviously, you copied yours directly from the message as did I. I am
curious as to why yours moved the "dynamicObj" to the next line after
the "//" and mine didn't. I would have expected the same thing with the
"Console.WriteLine" lines, since they started in a later column than the
"dynamicObj" line.

I am using Netscape 6 for my newgroups. Maybe it handles things
differently than yours.

Tom.


Nov 15 '05 #9
Thomas Scheiderich <tf*@deltanet.com> wrote:
That's interesting.

Obviously, you copied yours directly from the message as did I. I am
curious as to why yours moved the "dynamicObj" to the next line after
the "//" and mine didn't. I would have expected the same thing with the
"Console.WriteLine" lines, since they started in a later column than the
"dynamicObj" line.

I am using Netscape 6 for my newgroups. Maybe it handles things
differently than yours.


It could be a server on the way. Or Netscape. Or any number of things
:)

Are any of your lines over 80 columns long? If so, that may well be the
problem - try to keep everything to under 80 columns and it should be
fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

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

Similar topics

6
by: cppaddict | last post by:
Hi, I know that C++ does not have an explicit super() constructor for calling a Base class constructor from a Derived class's constructor, but my understanding is that C++ implements this...
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) .
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
6
by: Ook | last post by:
Can some kind soul explain this line? I'm not quite sure what the different parts do and exactly how it works. public: // Constructors Zoot(int size = 0) : _size(size), _data(_size ? new int...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
8
by: Jess | last post by:
Hello, When I define default constructors, I tend to use constructor initializers for member data. However, I was told the order in which members are initialized is determined by the order of...
4
by: Deep | last post by:
Can I use a class in this manner, where a constructor is of templated: template<typename T> class my_class{ public: int x_; public: template<typename U> my_class(const my_class<U>& other ) :...
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,...
1
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...
0
isladogs
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...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.