PropertyHasChanged()

PropertyHasChanged()

Old forum URL: forums.lhotka.net/forums/t/4019.aspx


ataylorm posted on Monday, December 10, 2007

It must be the crazy schedule or something, but three of us have been sitting here with our Expert C# 2005 Business Objects books and our code and we can't seem to figure out why this isn't working...  Any assistance so we can go home to our wifes/kids would be great.  Thanks

The code below is generating the following error on PropertyHasChanged() in the firstname property set code.

Error 1 Cannot access protected member 'Csla.Core.BusinessBase.PropertyHasChanged()' via a qualifier of type 'ProjectUnicorn.Library.UMUser.UMUserAccount'; the qualifier must be of type 'ProjectUnicorn.Library.UMUser.UMUserAccount' (or derived from it) C:\Code\Project Unicorn\ProjectUnicorn.Library\UMUser.cs 119 21 ProjectUnicorn.Library

public class UMUser : Csla.BusinessBase<UMUser>

{

      public class UMUserAccount

      {

               public string FirstName
              {
               
get { return _FirstName; }
               
set {
                        PropertyHasChanged();
                        
_FirstName = value; }
                 }

        }

         protected override void AddBusinessRules()
         {
                  ValidationRules.AddRule(
new RuleHandler(CommonRules.StringMaxLength),
                  new CommonRules.MaxLengthRuleArgs("Address1", 50));
                  ValidationRules.AddRule(
new RuleHandler(CommonRules.StringMaxLength),
                  new CommonRules.MaxLengthRuleArgs("FirstName", 10));
         }

         protected override void PropertyHasChanged(string propertyName)
         {
                  base.PropertyHasChanged(propertyName);
           }

}

DocJames replied on Tuesday, December 11, 2007

Hey,

Why do you have the properties inside it's own class?

Shouldn't you remove the "public class UMUserAccount" declaration?

Also, why do you override PropertyHasChanged and just call the base?

Thanks,

Jimmy

danielmartind replied on Tuesday, December 11, 2007

Hi,

 

I am quite new at this but in addition to DocJames comments I think you need to put the PropertyHasChanged() method at the end of the set method to read as follows

 public string FirstName
              {
               
get { return _FirstName; }
               
set {
                         
_FirstName = value;

                  PropertyHasChanged();

                   }
                 }

tmg4340 replied on Tuesday, December 11, 2007

Well - first off, I'm going to agree with previous folks on the head-scratcher part - you don't need to override "PropertyHasChanged" if you're just going to call the base version.

Beyond that, there are a couple of things I see with the code you've posted: 

1. If you're not going to specify the name of the property in the "PropertyHasChanged" call, then you need to decorate your setters & getters with the "NoInlining" attribute.  It's not in the book, but it's been discussed somewhat extensively on this website.  Check out this link for a brief explanation:

http://forums.lhotka.net/forums/thread/5741.aspx

However, as you'll notice in Rocky's statements, that shouldn't cause your program to throw an exception.  I've never done it that way, so I can't say for sure.  But you either need to provide the attributes, or else you need to explicity provide the name of the property in the "PropertyHasChanged" call.  I'd suggest the latter, if for no other reason than Rocky is deprecating the no-parameter version in the latest version of CSLA.

2. I'm not exactly sure how "UMUserAccount" is used, but if you're going to expect it to participate in all the CSLA goodness, it needs to derive from one of the CSLA base classes.  Given your sample, I'm presuming that it should be a BusinessBase-derived class.  Can't tell whether it is a child class from this sample, though it appears it may be that as well.  Your code may be "working" (i.e. compiling) because UMUserAccount is nested within a CSLA-derived class, and thus is given access to methods in "UMUser" that it normally wouldn't have.

HTH

ataylorm replied on Tuesday, December 11, 2007

Thanks for all the info.  We are still trying to get our heads around the CSLA as none of us have used it before and we are working on an impossible schedule.  (What's new :)  ).

The reason UMUserAccount is a child of UMUser is because the boss doesn't want us loading more into memory than absolutely required.  So UMUserAccount, UMUserProfile, etc. won't be "populated" unless they are created and a populate method is called.

So basically UMUser contains properties such as UserID, UserName, Password, etc. that are loaded when the user signs in.  If they go to their user account page, that is when UMUserAccount is created and populated.  Make sense?

I took out the override of PropertyHasChanged() and I tried:

public class UMUserAccount : Csla.BusinessBase<UMUserAccount>

however that wouldn't build.  Any ideas?

I REALLY appreciate all the help.

Andrew

 

ataylorm replied on Tuesday, December 11, 2007

Hey Guys,

Thanks for the help, I got it working great!  All I need now is to figure out how to do it with a structure.  :)

Thanks!

Copyright (c) Marimer LLC