Results 1 to 7 of 7

Thread: Updating Class Variable

  1. #1

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Updating Class Variable

    Hello everyone,

    I have a class holding information, which implements INotifyPropertyChanged. Everything works fine so far, everything gets updated when a change is made.

    But my problem starts with this piece of code:

    csharp Code:
    1. //List of TransActionProduct -- This is the class I mentioned
    2. ObservableCollection<Transactions.TransactionProduct> _PackageProducts = new ObservableCollection<SuperMarket.Data.Transactions.TransactionProduct>();
    3.  
    4. //This property adds up all the prices in the _PackageProducts List and returns it
    5.         decimal _price;
    6.         public decimal Price
    7.         {
    8.             get
    9.             {
    10.                 for (int i = 0; i < PackageProducts.Count; i++)
    11.                 {
    12.                     _price = _price + PackageProducts[i].TransactionProductPrice;
    13.                 }                
    14.                 return _price;
    15.                
    16.             }
    17.             set
    18.             {
    19.  
    20.                 _price = value;
    21.  
    22.                 //This class also implements INotifyPropertyChanged        
    23.                 NotifiyChange("Price");    
    24.             }
    25.            
    26.         }

    The problem is that while changing the prices of the TransactionProduct the Price property from the class above doesn't get updated.

    Does anyone know how can I make that property update while the TransactionProduct's price changes ?
    C# and WPF developer
    My Website:
    http://singlebits.com/

  2. #2
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: Updating Class Variable

    Where does the TransactionProduct get updated? In the UI?
    Why not use a datatrigger - I can't really see a the whole picture from what you posted but that seems like it might work.
    Anti DUPLO machine!!!

  3. #3

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: Updating Class Variable

    Yes, TransactionProduct gets updated in the UI.
    As I said, TransactionProduct is working great, the problem is my other class, which is a collection of TransactionProducts and a property "Price" which holds the total price of the TransactionProducts in the collection.

    Thanks for your response.
    C# and WPF developer
    My Website:
    http://singlebits.com/

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Updating Class Variable

    I'm not sure why there's a set on the Price property for a collection... especially when it doesn't then also update the list... to me, that's the kind of thing that should be a read only property.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: Updating Class Variable

    You are right, but what would you suggest I should do to update the Price every time one Transaction in the collection changes ?
    C# and WPF developer
    My Website:
    http://singlebits.com/

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Updating Class Variable

    How are you determining whether or not the Price has been updated? My guess is that you are displaying the Price in a textblock or something on the window that is loaded once and is then not refreshed after the transactions have been changed. If you update one of the transactions and then just do a MessageBox.Show to display the price property, does that show the updated value? I'm guessing it will do.

    Its all very well implementing INotifyChanged but when you just update an item in your observable collection, your Price property is not actually being set so this line will never get reached:
    Code:
     NotifiyChange("Price");
    So the change notification will never occur.

    Having said that, I'm not sure what the best way to resolve this would be... It depends how your program is structured really because the easiest way that I can see is to just have a public method in your class that raises the change notification and just call that each time you update a transaction, but that might not really be possible in your program (and there might be a better way)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: Updating Class Variable

    Thanks for your suggestion, I'll try it and let you know.
    C# and WPF developer
    My Website:
    http://singlebits.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width