Magic Instantiation! Most Annoying! :-(

  • Thread starter Thread starter thechaosengine
  • Start date Start date
T

thechaosengine

Hi all,

I get the following problem crop up every once in a while. I'm wondering
if anyone can tell me how to avoid it because its driving me absolutely insane!

I have a Project class that contains a User property amongst lots of other
properties.

In the Project class's constructor I initialise all the class's parameter
*apart from* the User property. The magic is that at any point after calling
Projects constructor, the User property has also been initialised, when it
should still be null because I've never assigned to it. Indeed I don't want
it to be initialised until I explicitly ask for it

I want to load the User object only when required - aka lazy loading. So
I have the following code:

public User ProjectManager{
get{
if(projectManager != null){
// Initialise the project manager
}

return projectManager;
}
}


Of course, because of the annoying instantiation problem the if statement
always decides that the projectManager variable is not null.


If its any more help, when I look at the initialised User object I can see
in the debugger that it has got all default values for each of its variables.

I've also tried putting a break point in the two user constructors but they
never fire.

This crap never happens with strings and doesnt always happen with custom
classes. Can anyone help as to why this is happening and what I can do to
get my lazy loading working?

Many thanks to anyone who can advise

Kindest Regards

tce
 
Is the User type a struct?

can you post a short example that is compilable by us that deomonstrates the behavior?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi all,

I get the following problem crop up every once in a while. I'm wondering
if anyone can tell me how to avoid it because its driving me absolutely insane!

I have a Project class that contains a User property amongst lots of other
properties.

In the Project class's constructor I initialise all the class's parameter
*apart from* the User property. The magic is that at any point after calling
Projects constructor, the User property has also been initialised, when it
should still be null because I've never assigned to it. Indeed I don't want
it to be initialised until I explicitly ask for it

I want to load the User object only when required - aka lazy loading. So
I have the following code:

public User ProjectManager{
get{
if(projectManager != null){
// Initialise the project manager
}

return projectManager;
}
}


Of course, because of the annoying instantiation problem the if statement
always decides that the projectManager variable is not null.


If its any more help, when I look at the initialised User object I can see
in the debugger that it has got all default values for each of its variables.

I've also tried putting a break point in the two user constructors but they
never fire.

This crap never happens with strings and doesnt always happen with custom
classes. Can anyone help as to why this is happening and what I can do to
get my lazy loading working?

Many thanks to anyone who can advise

Kindest Regards

tce
 
hi tce,

the problem can be the debugger itself. The watch windows show fields
and *properties* from a selected class. For the properties, the
debugger obviously has to run some code - which you probably cannot
debug (never tried that)

Try to run you program without a debugger and put a Console.WriteLine
in your properties' get accessor; or close all watch windows that show
properties.
 
Hello thechaosengine,

In your class, are you declaring projectManager like this:

public class MyClass
{
private User projectManager = new User();

public User ProjectManager
{
get
{
// your code in here...
}
}
}

If so, this explains it, since the new User() line is actually executed in
the static constructor...
 
No it isn't as the projectManagetr private field is not markeed as static

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

Hello thechaosengine,

In your class, are you declaring projectManager like this:

public class MyClass
{
private User projectManager = new User();

public User ProjectManager
{
get
{
// your code in here...
}
}
}

If so, this explains it, since the new User() line is actually executed in
the static constructor...

--
Matt Berther
http://www.mattberther.com
Hi all,

I get the following problem crop up every once in a while. I'm
wondering if anyone can tell me how to avoid it because its driving me
absolutely insane!

I have a Project class that contains a User property amongst lots of
other properties.

In the Project class's constructor I initialise all the class's
parameter *apart from* the User property. The magic is that at any
point after calling Projects constructor, the User property has also
been initialised, when it should still be null because I've never
assigned to it. Indeed I don't want it to be initialised until I
explicitly ask for it

I want to load the User object only when required - aka lazy loading.
So I have the following code:

public User ProjectManager{
get{
if(projectManager != null){
// Initialise the project manager
}
return projectManager;
}
}
Of course, because of the annoying instantiation problem the if
statement always decides that the projectManager variable is not null.

If its any more help, when I look at the initialised User object I can
see in the debugger that it has got all default values for each of its
variables.

I've also tried putting a break point in the two user constructors but
they never fire.

This crap never happens with strings and doesnt always happen with
custom classes. Can anyone help as to why this is happening and what I
can do to get my lazy loading working?

Many thanks to anyone who can advise

Kindest Regards

tce


--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.13 - Release Date: 16/01/2005



[microsoft.public.dotnet.framework]
 
Hello Richard Blewett [DevelopMentor],

Clearly... What I meant to say was the new User() line is actually executed
by the constructor.

--
Matt Berther
http://www.mattberther.com
No it isn't as the projectManagetr private field is not markeed as
static

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<765499563
(e-mail address removed)>

Hello thechaosengine,

In your class, are you declaring projectManager like this:

public class MyClass
{
private User projectManager = new User();
public User ProjectManager
{
get
{
// your code in here...
}
}
}
If so, this explains it, since the new User() line is actually
executed in
the static constructor...
--
Matt Berther
http://www.mattberther.com
Hi all,

I get the following problem crop up every once in a while. I'm
wondering if anyone can tell me how to avoid it because its driving
me absolutely insane!

I have a Project class that contains a User property amongst lots of
other properties.

In the Project class's constructor I initialise all the class's
parameter *apart from* the User property. The magic is that at any
point after calling Projects constructor, the User property has also
been initialised, when it should still be null because I've never
assigned to it. Indeed I don't want it to be initialised until I
explicitly ask for it

I want to load the User object only when required - aka lazy loading.
So I have the following code:

public User ProjectManager{
get{
if(projectManager != null){
// Initialise the project manager
}
return projectManager;
}
}
Of course, because of the annoying instantiation problem the if
statement always decides that the projectManager variable is not
null.
If its any more help, when I look at the initialised User object I
can see in the debugger that it has got all default values for each
of its variables.

I've also tried putting a break point in the two user constructors
but they never fire.

This crap never happens with strings and doesnt always happen with
custom classes. Can anyone help as to why this is happening and what
I can do to get my lazy loading working?

Many thanks to anyone who can advise

Kindest Regards

tce
--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.13 - Release Date:
16/01/2005
[microsoft.public.dotnet.framework]
 
Hi everyone,

Thanks so much for you're help so far. I very much appreciate it.

The Project class that contains the project manager is like this:

public class Project{
...
User projectManager = null;
...

public User ProjectManager{
get{
if(projectManager == null){
projectManager = // Get ProjectManager Call
}
}
}


public Project(){
// Doesnt touch the project manager variable
}
}


It does seem as though this code is getting called by the debugger:

public User ProjectManager{
get{
if(projectManager == null){
projectManager = // Get ProjectManager Call
}
}
}


However, the // Get ProjectManager Call calls a method that goes to the database
and I'm sure it isnt really calling the database.

Does this help?

I really need to get it to stop doing this as I want Lazy Loading. I don't
know how else to do it though

:-(

Thanks everyone.

I appreciate your help

Kindest Regards

tce
 
Hi everyone,

Thanks so much for you're help so far. I very much appreciate it.

The Project class that contains the project manager is like this:

public class Project{
...
User projectManager = null;
...

public User ProjectManager{
get{
if(projectManager == null){
projectManager = // Get ProjectManager Call
}
}
}


public Project(){
// Doesnt touch the project manager variable
}
}


It does seem as though this code is getting called by the debugger:

public User ProjectManager{
get{
if(projectManager == null){
projectManager = // Get ProjectManager Call
}
}
}


However, the // Get ProjectManager Call calls a method that goes to the database
and I'm sure it isnt really calling the database.

Does this help?

I really need to get it to stop doing this as I want Lazy Loading. I don't
know how else to do it though

:-(

Thanks everyone.

I appreciate your help

Kindest Regards

tce
 
Back
Top