473,666 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(projectManag er != 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 1225
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(projectManag er != 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.WriteLi ne
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(projectManag er == 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(projectManag er == 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(projectManag er != 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
1686
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
1704
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 ); Stroustrup said **** line is wrong because alternative resolution of f( t )
12
2612
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
4
1987
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 working fine but when I try to instantiate an XmlSerializer object with: Dim xmls As New XmlSerializer(GetType(CLayerDefinition)) I get the following error:
13
1695
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." here: http://jibbering.com/faq/faq_notes/closures.html
3
4004
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 handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
16
2591
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 previous python experiences: magic names (I felt almost as sad as when I discovered the strange pink worms that eat you in nethack, not to mention the mind flayers - I really hate them). I guess all programming languages have magic names to some...
2
1871
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
3673
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
2487
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
8363
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8561
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8645
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5672
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
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 we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.