473,324 Members | 2,400 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,324 software developers and data experts.

class member initialization

Dear all,
if I initialize member like this
class A
{
pen _mypen= new pen(...)

}
and class A has 3 constructors will pen be initialized in all of them?
Is there any documentation about this issue?
Thanks in advane,
Boni
Nov 17 '05 #1
6 2148
Hi Boni,
al instance members will be initialized before the constructor is
executed. For example:

class SomeClass
{
public SomeClass()
{
Console.WriteLine("SomeClass being created");
}
}

class MyClass
{
private SomeClass _member = new SomeClass();

public MyClass()
{
Console.WriteLine("MyClass being created");
}
}

and you do the following:

MyClass x = new MyClass();

you would see in the standard output:
MyClass being created
SomeClass beign created

In short all member instances will be initalized before any constructor gets
called.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"Boni" wrote:
Dear all,
if I initialize member like this
class A
{
pen _mypen= new pen(...)

}
and class A has 3 constructors will pen be initialized in all of them?
Is there any documentation about this issue?
Thanks in advane,
Boni

Nov 17 '05 #2
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> skrev i en
meddelelse news:85**********************************@microsof t.com...
Hi Boni,
al instance members will be initialized before the constructor is
executed. For example:

class SomeClass
{
public SomeClass()
{
Console.WriteLine("SomeClass being created");
}
}

class MyClass
{
private SomeClass _member = new SomeClass();

public MyClass()
{
Console.WriteLine("MyClass being created");
}
}

and you do the following:

MyClass x = new MyClass();

you would see in the standard output:
MyClass being created
SomeClass beign created

In short all member instances will be initalized before any constructor
gets
called.


Don't you mean that the output will be:
SomeClass beign created
MyClass being created
Nov 17 '05 #3
thanks - yes I did. When you are late and in a rush that is the kind of
thing that happens :-)

"Peter Kirk" wrote:
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> skrev i en
meddelelse news:85**********************************@microsof t.com...
Hi Boni,
al instance members will be initialized before the constructor is
executed. For example:

class SomeClass
{
public SomeClass()
{
Console.WriteLine("SomeClass being created");
}
}

class MyClass
{
private SomeClass _member = new SomeClass();

public MyClass()
{
Console.WriteLine("MyClass being created");
}
}

and you do the following:

MyClass x = new MyClass();

you would see in the standard output:
MyClass being created
SomeClass beign created

In short all member instances will be initalized before any constructor
gets
called.


Don't you mean that the output will be:
SomeClass beign created
MyClass being created

Nov 17 '05 #4
Dear Sirs,
the reason of my question is a problem in my "real" application. But I can't
reproduce it in small examples.
May be you have any ideas why this happens.
In my understanding if I initialize member like this:
private SomeClass _member = new SomeClass();
than compiler will move this initialization into all constructors. (And it
is what compiler does on small examples).
But in "real" applicaton, compiler moves the initialization into one
constructor only (I think default constructor). And if I create an object
with other constructors, then _member is null.
Can somebody help me to figure out, what the reason is?
Thank you in advance,
Boni

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> schrieb im
Newsbeitrag news:90**********************************@microsof t.com...
thanks - yes I did. When you are late and in a rush that is the kind of
thing that happens :-)

"Peter Kirk" wrote:
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> skrev i en
meddelelse news:85**********************************@microsof t.com...
> Hi Boni,
> al instance members will be initialized before the constructor is
> executed. For example:
>
> class SomeClass
> {
> public SomeClass()
> {
> Console.WriteLine("SomeClass being created");
> }
> }
>
> class MyClass
> {
> private SomeClass _member = new SomeClass();
>
> public MyClass()
> {
> Console.WriteLine("MyClass being created");
> }
> }
>
> and you do the following:
>
> MyClass x = new MyClass();
>
> you would see in the standard output:
> MyClass being created
> SomeClass beign created
>
> In short all member instances will be initalized before any constructor
> gets
> called.


Don't you mean that the output will be:
SomeClass beign created
MyClass being created

Nov 17 '05 #5
Boni <oilia@nospam> wrote:
the reason of my question is a problem in my "real" application. But I can't
reproduce it in small examples.
How far have you got with taking the "problem" version and removing a
bit at a time?
May be you have any ideas why this happens.
In my understanding if I initialize member like this:
private SomeClass _member = new SomeClass();
than compiler will move this initialization into all constructors. (And it
is what compiler does on small examples).
Yes - it's not quite as if it moves it into the first line of code in
your constructor; it calls the member initializers first, then calls
the base class constructor, then calls any code you've specified in
your constructor.

Does your base class constructors call any virtual methods which are
overridden in the derived class?
But in "real" applicaton, compiler moves the initialization into one
constructor only (I think default constructor). And if I create an object
with other constructors, then _member is null.
Can somebody help me to figure out, what the reason is?


I suspect you're not seeing what I think you're seeing. Have you tried
putting a breakpoint on it?

Unfortunately it's very hard to work out what's wrong without a test
case.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Dear Jon.Thanks for your help. I found out, what the problem was.
"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP************************@msnews.microsoft.c om...
Boni <oilia@nospam> wrote:
the reason of my question is a problem in my "real" application. But I
can't
reproduce it in small examples.


How far have you got with taking the "problem" version and removing a
bit at a time?
May be you have any ideas why this happens.
In my understanding if I initialize member like this:
private SomeClass _member = new SomeClass();
than compiler will move this initialization into all constructors. (And
it
is what compiler does on small examples).


Yes - it's not quite as if it moves it into the first line of code in
your constructor; it calls the member initializers first, then calls
the base class constructor, then calls any code you've specified in
your constructor.

Does your base class constructors call any virtual methods which are
overridden in the derived class?
But in "real" applicaton, compiler moves the initialization into one
constructor only (I think default constructor). And if I create an object
with other constructors, then _member is null.
Can somebody help me to figure out, what the reason is?


I suspect you're not seeing what I think you're seeing. Have you tried
putting a breakpoint on it?

Unfortunately it's very hard to work out what's wrong without a test
case.

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

Nov 17 '05 #7

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

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
6
by: Fred | last post by:
Hi I have a class defined in a library that I'd like to add some extra functionality to. This will involve adding a few member variables and a few related methods. As I understand it I can...
11
by: DrNoose | last post by:
Hi! I've got a program that's almost done, but I'm getting compile errors in two lines: 317 & 319. I think the main error has to do with the Truck Class. I'm a newbie and keep looking at the...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.