I have used secondary objects to hold original property values to check for changes and flag the user if the new value(s) do not match the original values. I once saw an example where someone implemented IEditable and used a struct for both the property backing and a copy of the original values.
I am a bit fuzzy on when and why to use a struct beyond what I read in MS documentation and it would seem that a struct should not be used in this case. I was wondering if anyone has had any experience in this area or has a slick way to compare values and flag is dirty when they change and unflag isdirty if it is changed back to the original value. I was just looking at a Struct for performance/GC reasons, but I can easily continue down the road I have been on with this.
structure is similar to class, but light weighted. It is useful where huge number of memory instances are required, sometime structure does not need instance/object to be initialised: which means values can be directly accessed inside a structure where in a class you need the instance of the class to access the values from memory.
Hi lat Defense.
I think Joon84 has showed the correct explaination for you.
What difference between structure and class is the Encapsulation. As for storing data, using class will be more suitable.
if you have any specifical situaction for yours, you need to show it to us.
No code, No fact.
I guess to summarize my post, my two questions are:
Is a struct a good way to maintain the original values in an object to use for comparison to determine if data changes have made that object "dirty". If it is, is this the best way or does someone have a better way to identify an object as "dirty" when a user changes a value. This includes when they change it back to its original value and it is no longer dirty.
Also, is a struct acceptable to use for property backing. I had read somewhere that a struct values should never change and this is where things get a little fuzzy for me because I see examples of it being used for all kinds of things where its values are being changed directly or indirectly by users.
I have objects with multiple properties and a change to any of those properties would make that object dirty. I was thinking of using two structs; one to hold the original values and one that holds the values the properties reference. I just don't know if i am shooting myself in the foot by doing this.
The best solution to track a object's state-change is to save it's state in the database and compare the new value with the one saved in the database.
Every custom solution is different, so it is hard to comment what is best for you. In general, classes can be used when you have more complex behavior or data. And if you think that these behavior or data to be modified after creating an instance of class, then classes are the best fit. Structures can be used for
small data elements. If developer feels that data members of structure cannot to be modified after creating structure, then having structure will suit.
- Proposed as answer by Enamul Kh-MSFTMicrosoft employee Tuesday, August 28, 2012 5:49 PM