Re: Magic Instantiation! Most Annoying! :-(

From: Richard Blewett [DevelopMentor] (richardb_at_NOSPAMdevelop.com)
Date: 01/16/05


To: microsoft.public.dotnet.general,microsoft.public.dotnet.framework,microsoft.public.dotnet.languages.csharp
Date: Sun, 16 Jan 2005 10:04:18 -0800

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



Relevant Pages

  • Magic Instantiation! Most Annoying! :-(
    ... I get the following problem crop up every once in a while. ... I have a Project class that contains a User property amongst lots of other ... In the Project class's constructor I initialise all the class's parameter ... Projects constructor, the User property has also been initialised, when it ...
    (microsoft.public.dotnet.languages.csharp)
  • Magic Instantiation! Most Annoying! :-(
    ... I get the following problem crop up every once in a while. ... I have a Project class that contains a User property amongst lots of other ... In the Project class's constructor I initialise all the class's parameter ... Projects constructor, the User property has also been initialised, when it ...
    (microsoft.public.dotnet.framework)
  • Magic Instantiation! Most Annoying! :-(
    ... I get the following problem crop up every once in a while. ... I have a Project class that contains a User property amongst lots of other ... In the Project class's constructor I initialise all the class's parameter ... Projects constructor, the User property has also been initialised, when it ...
    (microsoft.public.dotnet.general)
  • Re: Magic Instantiation! Most Annoying! :-(
    ... I have a Project class that contains a User property amongst lots of other ... In the Project class's constructor I initialise all the class's parameter ... Projects constructor, the User property has also been initialised, when it ... Kindest Regards ...
    (microsoft.public.dotnet.framework)
  • Re: Magic Instantiation! Most Annoying! :-(
    ... I have a Project class that contains a User property amongst lots of other ... In the Project class's constructor I initialise all the class's parameter ... Projects constructor, the User property has also been initialised, when it ... Kindest Regards ...
    (microsoft.public.dotnet.languages.csharp)