473,385 Members | 1,829 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,385 software developers and data experts.

Magic Instantiation! Most Annoying! :-(

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

Jul 21 '05 #1
3 1206
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

Jul 21 '05 #2
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.

Jul 21 '05 #3
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 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

Jul 21 '05 #4

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

Similar topics

7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
7
by: Hunter Hou | last post by:
Hello, I'm trying one example of <<the C++ programming language>> Page 865 int f( int ); template< class T > T g( T t ) { return f( t ); } char c = g( 'a' ); ************ char f( char ); ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
4
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
16
by: per9000 | last post by:
Hi, I recently started working a lot more in python than I have done in the past. And I discovered something that totally removed the pretty pink clouds of beautifulness that had surrounded my...
2
by: WittyGuy | last post by:
Hi My class looks something like this: class Base { public: Base () {} ~Base () {} // No virtual dtor in the base class private: };
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.