By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Property Modified (dirty)

P: n/a
Hi,
I need to know when a property has ben modified (IsDirty).
The most obvious way to solve the problem is perhaps something like the
following;

Private m_sFirstName As String
Private m_bFirstName_changed As Boolean = False
Public Property FirstName() As String
Get
Return m_sFirstName
End Get
Set(ByVal Value As String)
If m_sFirstName = Value Then Return
m_sFirstName = Value
m_bFirstName_changed = True
End Set
End Property

In my case threre are hundreds of properties that need this
functionality and this solution has many drawbacks.
Does anyone have a better solution?
Thanks in advance
/Dan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Dan,

I can't see a solution based on the information you provided. Could you
explain a little more what you are trying to do. What happens when a
property is dirty? Does an entire record get sent to the database? Or a
database routine is run that just updates that one property? Do you present
a confirmation screen highlighting just the properties that have changed?
Something totally different? Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Dan Iregren" <da*@iregren.com> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
Hi,
I need to know when a property has ben modified (IsDirty).
The most obvious way to solve the problem is perhaps something like the
following;

Private m_sFirstName As String
Private m_bFirstName_changed As Boolean = False
Public Property FirstName() As String
Get
Return m_sFirstName
End Get
Set(ByVal Value As String)
If m_sFirstName = Value Then Return
m_sFirstName = Value
m_bFirstName_changed = True
End Set
End Property

In my case threre are hundreds of properties that need this
functionality and this solution has many drawbacks.
Does anyone have a better solution?
Thanks in advance
/Dan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #2

P: n/a
Something like this (just an idea - untested):

Private Function PropertyIsDirty( _
ByVal PreviousValue As Object, _
ByVal Value As Object) As Boolean

Dim IsDirty As Boolean

If TypeOf Value Is System.ValueType Then
Select Case VarType(Value)

Case VariantType.String
If Not Ctype(PreviousValue, String).Equals( _
Ctype(Value, String)) Then
IsDirty = True
End If

Case VariantType.Integer, VariantType.Long, _
VariantType.Single, VariantType.Double
If Not Ctype(PreviousValue, _
System.ValueType).Equals(Value) Then
IsDirty = True
End If

' add other cases..

End Select
End If
End function

Private m_sFirstName As String
Private m_IsDirtyHash As New HashTable

For Each prop As PropertyInfo In Me.GetType.GetProperties
m_IsDirtyHash.Add(prop.Name, False)
Next prop

Public Property FirstName() As String
Get
Return m_sFirstName
End Get
Set(ByVal Value As String)
m_IsDirtyHash.Item("FirstName") = _
PropertyIsDirty(m_sFirstName, Value)
If CType(m_IsDirtyHash.Item("FirstName"), _
Boolean) Then
m_sFirstName = Value
End Set
End Property
Or maybe you could implement an interface to group all your
properties...something like this:
Interface Properties
Property myproperty(key As Integer) as Object

ReadOnly Property dirty(index As Integer) as Boolean
End Interface

class class1
implements Properties
private const MAX_PROPERTY_NUM = 20
private m_prop(MAX_PROPERTY_NUM - 1) as Object
private m_dirty(MAX_PROPERTY_NUM - 1) as boolean

public property myprop(index as Integer) as Object _
implements Properties.myproperty
Get
Return m_prop(index)
End Get
Set(ByVal Value as Object)
m_dirty(index) = PropertyIsDirty(Index, Value)
m_prop(index) = Value
End Set
end property

public readonly property dirty(index as Integer) as boolean implements
Properties.dirty
Get
Return m_dirty(index)
End Get
end property

private function PropertyIsDirty( _
ByVal Index as Integer, _
ByVal Value As Object)
If TypeOf Value Is System.ValueType Then
Select Case VarType(Value)
Case VariantType.String
If Not Ctype(m_prop(Index), _
String).Equals(Ctype(Value, String)) Then
m_dirty(Index) = True
End If
Case VariantType.Integer, VariantType.Long, _
VariantType.Single, VariantType.Double
If Not Ctype(m_prop(Index), _
System.ValueType).Equals(Value) Then
m_dirty(Index) = True
End If

' add other cases..

End Select
End If
end function
end class
hope that helps..
Imran.

"Dan Iregren" <da*@iregren.com> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
Hi,
I need to know when a property has ben modified (IsDirty).
The most obvious way to solve the problem is perhaps something like the
following;

Private m_sFirstName As String
Private m_bFirstName_changed As Boolean = False
Public Property FirstName() As String
Get
Return m_sFirstName
End Get
Set(ByVal Value As String)
If m_sFirstName = Value Then Return
m_sFirstName = Value
m_bFirstName_changed = True
End Set
End Property

In my case threre are hundreds of properties that need this
functionality and this solution has many drawbacks.
Does anyone have a better solution?
Thanks in advance
/Dan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #3

P: n/a
Ok, the code is from the "Business layer", on top of a SqlServer.

Let us say the property belongs to a class named Person.

A person can be created uninitialized;
Dim p as New Person()

partly initialized;
Dim p as new Person()
p.FirstName = "Dan"

or fully initialized (from the database);
Dim p as new Person()
p.InitFromDataReader(l_DataReader)
..

I save a Person to the database by rendering sql code and executing a
SqlCommand.
If the Person exists in the database I will render an update command.
If the Person is not fully initialized I can not update all its
properties to the database (since that might overwrite data), so this is
why I need a way to tell if each property is dirty or not.
/Dan


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #4

P: n/a
Dan,
I would make m_sFirstName itself an object that knew if it was dirty or not.

Something like:

Public Structure StringHolder

Private m_value As String
Private m_isDirty As Boolean

Public Property Value() As String
Get
Return m_value
End Get
Set(ByVal value As String)
If m_value = value Then Return
m_value = value
m_isDirty = True
End Set
End Property

Public ReadOnly Property IsDirty() As Boolean
Get
Return m_isDirty
End Get
End Property

End Structure

Private ReadOnly m_firstName As StringHolder

Public Property FirstName() As String
Get
Return m_firstName.Value
End Get
Set(ByVal value As String)
m_firstName.Value = value
End Set
End Property

You could either make StringHolder hold Objects directly if boxing was not a
concern, or simply create StringHolder like structures for other types (such
as IntegerHolder, DateTimeHolder...) I would consider adding support so a
StringHolder could notify its "parent" that the property was modified. I
would also consider adding support for ToString...

NOTE: In VS.NET 2005 (aka Whidbey, due out in 2005) I would make
StringProperty a generic class, something like:

' VS.NET 2005 sample
Public Structure PropertyHolder(Of T As IComparable(Of T))

Private m_value As T
Private m_isDirty As Boolean

Public Property Value() As T
Get
Return m_value
End Get
Set(ByVal value As T)
If m_value.Equals(value) Then Return
m_value = value
m_isDirty = True
End Set
End Property

Public ReadOnly Property IsDirty() As Boolean
Get
Return m_isDirty
End Get
End Property

End Structure

Then you can simply use PropertyHolder for each of your fields, instead of
defining StringHolder, IntegerHolder, DateTimeHolder, BooleanHolder...

' VS.NET 2005 sample
Private ReadOnly m_firstName As PropertyHolder(Of String)
Private ReadOnly m_lastName As PropertyHolder(Of String)
Private ReadOnly m_dob As PropertyHolder(Of DateTime)
Private ReadOnly m_id As PropertyHolder(Of Integer)
Private ReadOnly m_active As PropertyHolder(Of Boolean)

Hope this helps
Jay

"Dan Iregren" <da*@iregren.com> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
Hi,
I need to know when a property has ben modified (IsDirty).
The most obvious way to solve the problem is perhaps something like the
following;

Private m_sFirstName As String
Private m_bFirstName_changed As Boolean = False
Public Property FirstName() As String
Get
Return m_sFirstName
End Get
Set(ByVal Value As String)
If m_sFirstName = Value Then Return
m_sFirstName = Value
m_bFirstName_changed = True
End Set
End Property

In my case threre are hundreds of properties that need this
functionality and this solution has many drawbacks.
Does anyone have a better solution?
Thanks in advance
/Dan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #5

P: n/a
I should add, you could also extend StringHolder to support Original Value
verses Current Value.

Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
Dan,
I would make m_sFirstName itself an object that knew if it was dirty or
not.

Something like:

Public Structure StringHolder

Private m_value As String
Private m_isDirty As Boolean

Public Property Value() As String
Get
Return m_value
End Get
Set(ByVal value As String)
If m_value = value Then Return
m_value = value
m_isDirty = True
End Set
End Property

Public ReadOnly Property IsDirty() As Boolean
Get
Return m_isDirty
End Get
End Property

End Structure

Private ReadOnly m_firstName As StringHolder

Public Property FirstName() As String
Get
Return m_firstName.Value
End Get
Set(ByVal value As String)
m_firstName.Value = value
End Set
End Property

You could either make StringHolder hold Objects directly if boxing was not
a concern, or simply create StringHolder like structures for other types
(such as IntegerHolder, DateTimeHolder...) I would consider adding support
so a StringHolder could notify its "parent" that the property was
modified. I would also consider adding support for ToString...

NOTE: In VS.NET 2005 (aka Whidbey, due out in 2005) I would make
StringProperty a generic class, something like:

' VS.NET 2005 sample
Public Structure PropertyHolder(Of T As IComparable(Of T))

Private m_value As T
Private m_isDirty As Boolean

Public Property Value() As T
Get
Return m_value
End Get
Set(ByVal value As T)
If m_value.Equals(value) Then Return
m_value = value
m_isDirty = True
End Set
End Property

Public ReadOnly Property IsDirty() As Boolean
Get
Return m_isDirty
End Get
End Property

End Structure

Then you can simply use PropertyHolder for each of your fields, instead of
defining StringHolder, IntegerHolder, DateTimeHolder, BooleanHolder...

' VS.NET 2005 sample
Private ReadOnly m_firstName As PropertyHolder(Of String)
Private ReadOnly m_lastName As PropertyHolder(Of String)
Private ReadOnly m_dob As PropertyHolder(Of DateTime)
Private ReadOnly m_id As PropertyHolder(Of Integer)
Private ReadOnly m_active As PropertyHolder(Of Boolean)

Hope this helps
Jay

"Dan Iregren" <da*@iregren.com> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl...
Hi,
I need to know when a property has ben modified (IsDirty).
The most obvious way to solve the problem is perhaps something like the
following;

Private m_sFirstName As String
Private m_bFirstName_changed As Boolean = False
Public Property FirstName() As String
Get
Return m_sFirstName
End Get
Set(ByVal Value As String)
If m_sFirstName = Value Then Return
m_sFirstName = Value
m_bFirstName_changed = True
End Set
End Property

In my case threre are hundreds of properties that need this
functionality and this solution has many drawbacks.
Does anyone have a better solution?
Thanks in advance
/Dan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.