Results 1 to 5 of 5

Thread: [2005] Problems with custom classing / Default property

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Houston, TX
    Posts
    342

    Question [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:
    1. Public Class Customer
    2.     Private _Name As New CustomerName
    3.  
    4.     Public Property Name() As CustomerName
    5.         Get
    6.             Return _Name
    7.         End Get
    8.         Set(ByVal value As CustomerName)
    9.             _Name = value
    10.         End Set
    11.     End Property
    12.  
    13. End Class
    VB Code:
    1. Public Class CustomerName
    2.     Private _Original As String
    3.     Private _Current As String
    4.  
    5.     Public Property Original() As String
    6.         Get
    7.             Return _Original
    8.         End Get
    9.         Set(ByVal value As String)
    10.             _Original = value
    11.         End Set
    12.     End Property
    13.  
    14.     Public Property Current() As String
    15.         Get
    16.             Return _Current
    17.         End Get
    18.         Set(ByVal value As String)
    19.             _Current = value
    20.         End Set
    21.     End Property
    22.  
    23. 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!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Problems with custom classing / Default property

    It's simply not possible. Only properties that take an index can be the default.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Houston, TX
    Posts
    342

    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!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Enum DataVersion
    2.     Original
    3.     Current
    4. End Enum
    5.  
    6.  
    7. Public Class Customer
    8.  
    9.     Private Class CustomerData
    10.         Public Name As String
    11.  
    12.         Public Sub CopyTo(ByVal data As CustomerData)
    13.             data.Name = Me.Name
    14.         End Sub
    15.     End Class
    16.  
    17.     Private originalData As New CustomerData
    18.     Private currentData As New CustomerData
    19.  
    20.     Public Overloads Property Name() As String
    21.         Get
    22.             Return Me.currentData.Name
    23.         End Get
    24.         Set(ByVal value As String)
    25.             Me.currentData.Name = value
    26.         End Set
    27.     End Property
    28.  
    29.     Public Overloads ReadOnly Property Name(ByVal version As DataVersion) As String
    30.         Get
    31.             Dim value As String = Nothing
    32.  
    33.             Select Case version
    34.                 Case DataVersion.Original
    35.                     value = Me.originalData.Name
    36.                 Case DataVersion.Current
    37.                     value = Me.currentData.Name
    38.             End Select
    39.  
    40.             Return value
    41.         End Get
    42.     End Property
    43.  
    44.     Public Sub AcceptChanges()
    45.         Me.currentData.CopyTo(Me.originalData)
    46.     End Sub
    47.  
    48.     Public Sub RejectChanges()
    49.         Me.originalData.CopyTo(Me.currentData)
    50.     End Sub
    51.  
    52. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Houston, TX
    Posts
    342

    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.

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