[2005] Problems with custom classing / Default property
This one is a tricky to explain so I will try to use a lot of examples.
VB Code:
Public Class Customer
Private _Name As New CustomerName
Public Property Name() As CustomerName
Get
Return _Name
End Get
Set(ByVal value As CustomerName)
_Name = value
End Set
End Property
End Class
VB Code:
Public Class CustomerName
Private _Original As String
Private _Current As String
Public Property Original() As String
Get
Return _Original
End Get
Set(ByVal value As String)
_Original = value
End Set
End Property
Public Property Current() As String
Get
Return _Current
End Get
Set(ByVal value As String)
_Current = value
End Set
End Property
End Class
What I would like to do is set the property like:
Code:
Customer.Name="Brian"
without having to do:
Code:
Customer.Name.Current="Brian"
and then be able to access those extended properties so I can get the value of
Code:
Customer.Name.Original
when updating the dataset.
I think the easiest way of asking is how do I make the 'CURRENT' property the default?
Thanks!!
Re: [2005] Problems with custom classing / Default property
It's simply not possible. Only properties that take an index can be the default.
Re: [2005] Problems with custom classing / Default property
Thanks for the reply, I will figure out a different approach then.
What is the best way to keep track of changes in a class? Normally in my property set method I simply assign the current value to some variable and go back later to check to see if something has changed but I have a large class (about 30 properties which are subclasses) and the code would quicky get sloppy if I had to make individual properties for both current and original values.
Any point in the right direction is appreciated!!
Re: [2005] Problems with custom classing / Default property
How will it get sloppy? The code may get longer but long code is not necessarily sloppy. It's only sloppy if you write it badly.
Also, I would recommend against having separate classes for each property of your Customer class. I would think that the Customer class would contain all the data itself. You might want to consider implementing it something like this:
vb.net Code:
Public Enum DataVersion
Original
Current
End Enum
Public Class Customer
Private Class CustomerData
Public Name As String
Public Sub CopyTo(ByVal data As CustomerData)
data.Name = Me.Name
End Sub
End Class
Private originalData As New CustomerData
Private currentData As New CustomerData
Public Overloads Property Name() As String
Get
Return Me.currentData.Name
End Get
Set(ByVal value As String)
Me.currentData.Name = value
End Set
End Property
Public Overloads ReadOnly Property Name(ByVal version As DataVersion) As String
Get
Dim value As String = Nothing
Select Case version
Case DataVersion.Original
value = Me.originalData.Name
Case DataVersion.Current
value = Me.currentData.Name
End Select
Return value
End Get
End Property
Public Sub AcceptChanges()
Me.currentData.CopyTo(Me.originalData)
End Sub
Public Sub RejectChanges()
Me.originalData.CopyTo(Me.currentData)
End Sub
End Class
If this is a business object backed by records from a database then you could actually assign the DataRow it corresponds to to a private field and expose it via a property marked Friend. That way the record is accessible in your BLL but not your presentation layer.
Re: [2005] Problems with custom classing / Default property
That is what I was looking for. I want to make sure I track changes in the class because in previous applications I had to iterate through each property to to figure out what was differnet. I like using classes bound to a data source (I usually implement IBindingSource) but I am trying to get some of the functionality of datasets (such as rowstate).
Thanks for the example and I'm gonna try that approach now.