class member initialization

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

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
 
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
 
Mark R. Dawson said:
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
 
thanks - yes I did. When you are late and in a rush that is the kind of
thing that happens :-)
 
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
 
Boni said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top