6 Replies - 456 Views - Last Post: 22 September 2013 - 08:54 AM Rate Topic: -----

#1 ScottinTexas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 13-March 12

Can a class detect a change?

Posted 22 September 2013 - 06:07 AM

I have a class that represents a single book entry. Is there a built in method that will set a flag or a boolean or something that will tell me if the object has changed since it was instantiated. For example, if the user changes the title of the entry from 'Deal' to 'Big Deal' I want to be able to identify the object for saving the data to the database. But let me be more specific in case I am going about this all wrong. I am looking for something that is automatically triggered on change so I can say "if I have changed set this flag." Then I can bind that flag to a "SaveMe" button on the UI that the user will click when finished editing.

The object will be added to a collection when the proper filters are set for the data base. All the records in the database that belong to the collection will be gathered up and each one will be added to the collection. If any of the objects in the collection changes I need to know so it can update the database. I am binding the class to fields on the UI and it will be a two way binding, but when it comes to writing the data back to the disk I am not yet sure how I will do that. Also the user will be able to add a new item or delete one from the collection. So instead of waiting until everything is done and the user is about to exit, I need to update the DB when the class is finished being changed.

If you see me going about this wrong please say so. I am used to working with access and c# is new. I am working in Visual Studio 10 Prof. My database is already built and I use it everyday.

Is This A Good Question/Topic? 0
  • +

Replies To: Can a class detect a change?

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3746
  • View blog
  • Posts: 13,116
  • Joined: 12-December 12

Re: Can a class detect a change?

Posted 22 September 2013 - 06:16 AM

Look into the INotifyPropertyChanged Interface. However, have a look through the Tutorials here @DIC as well - there is a link at the top of this page. There may be a tutorial on this subject, or custom events.

There are a number of ways that data-binding can be set-up. Depending on how you are achieving this, the property-changed event may already be registered/implemented. However, from your description, it seems that you are doing this entirely yourself in code, so the previous link is appropriate.

This post has been edited by andrewsw: 22 September 2013 - 06:21 AM

Was This Post Helpful? 1
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Can a class detect a change?

Posted 22 September 2013 - 06:20 AM

This tutorial has a lot of good information in it:

WPF databinding

And also look towards the end of this tutorial

C# - Properties
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Can a class detect a change?

Posted 22 September 2013 - 08:07 AM

View PostScottinTexas, on 22 September 2013 - 09:07 AM, said:

Then I can bind that flag to a "SaveMe" button on the UI that the user will click when finished editing.

Good idea with the binding. That means you are learning the ways of WPF. :)

View PostScottinTexas, on 22 September 2013 - 09:07 AM, said:

I am looking for something that is automatically triggered on change so I can say "if I have changed set this flag."

Unfortunately, you can't get this automatically (yet) with the current version of the C# language/.NET framework. You'll still need to hook things up yourself. You could probably try to make this a little bit more hidden by writing your own custom attributes and injecting code, but you still have to put in the work to do that.
Was This Post Helpful? 0
  • +
  • -

#5 ScottinTexas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 13-March 12

Re: Can a class detect a change?

Posted 22 September 2013 - 08:16 AM

Thanks all for the rapid response. A brief look at INotifyPropertyChanged looks like each of the several properties would have to raise that. I'll do the serious reading on that and see if I am following it correctly. INotifyObjectChanged would be more appropriate.

Thanks again. I'll check out the tutorial also. Then, if needed I'll resurrect this thread.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5633
  • View blog
  • Posts: 12,086
  • Joined: 02-June 10

Re: Can a class detect a change?

Posted 22 September 2013 - 08:46 AM

I agree that this sounds like a good place to be doing this in WPF and then use ObservableCollection<T>. Its like an improved List<T> where notifications happen when there is a change to any of the elements.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Can a class detect a change?

Posted 22 September 2013 - 08:54 AM

Quote

You could probably try to make this a little bit more hidden by writing your own custom attributes and injecting code, but you still have to put in the work to do that.


That's basically what PostSharp does:

http://www.postsharp.net/ INotifyPropertyChanged example is on the front page.

Never used it but thought it looked interesting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1