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

Custom Object data binding, polymorphism and inheritance

P: 1
I'm hoping that someone out there can give me some guidance here, and I hope that I am explaining this properly.


Here is my scenerio:

I have a BaseData object that serves as an abstract object for all my business objects. Notice there is an overridable method on the object.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class BaseData
  3.  
  4.         Dim msDataID As String
  5.         Dim msDescription As String
  6.  
  7.  
  8.  
  9.         <DataMember()>
  10.         Property DataID() As String
  11.             Get
  12.                 DataID = msDataID
  13.             End Get
  14.             Set(ByVal value As String)
  15.                 msDataID = value
  16.             End Set
  17.         End Property
  18.  
  19.         Property Description() As String
  20.             Get
  21.                 Description = msDescription
  22.             End Get
  23.             Set(ByVal value As String)
  24.                 msDescription = value
  25.             End Set
  26.         End Property
  27.  
  28.             Public Overridable Function GetAllObjects() As BaseDataCollection
  29.  
  30.                     Dim colObjects As BaseDataCollection = Nothing
  31.  
  32.                     colObjects = (get data from data source)
  33.  
  34.                     Return colObjects
  35.             End Function
  36.  
  37.  
  38.  
  39. End Class
  40.  

Here is the base Collection that servers as the abstract collection for all my business object collections:

Expand|Select|Wrap|Line Numbers
  1. Public Class BaseDataCollection
  2.         Inherits BindingList(Of BaseData)
  3.         Implements IComponent
  4.  
  5. #Region "IComponent Implementation"
  6.         Private m_Site As ISite = Nothing
  7.         Public Event Disposed(ByVal sender As Object, ByVal e As System.EventArgs) _
  8.             Implements System.ComponentModel.IComponent.Disposed
  9.  
  10.         Protected Property Site() As System.ComponentModel.ISite Implements _
  11.             System.ComponentModel.IComponent.Site
  12.             Get
  13.                 Return m_Site
  14.             End Get
  15.             Set(ByVal Value As System.ComponentModel.ISite)
  16.                 m_Site = Value
  17.             End Set
  18.         End Property
  19.  
  20.         Public Sub Dispose() Implements System.IDisposable.Dispose
  21.             Me.Items.Clear()
  22.             RaiseEvent Disposed(Me, System.EventArgs.Empty)
  23.         End Sub
  24. #End Region
  25.  
  26. #Region "IBindingList Sorting Features"
  27.         Private m_SupportsSorting As Boolean = True
  28.         Private m_SortProperty As PropertyDescriptor
  29.         Private m_SortDirection As ListSortDirection
  30.         Private m_OriginalList As ArrayList
  31.  
  32.         Protected Overrides ReadOnly Property SupportsSortingCore() As Boolean
  33.             Get
  34.                 Return m_SupportsSorting
  35.             End Get
  36.         End Property
  37.         Protected Overrides ReadOnly Property SortDirectionCore() As System.ComponentModel.ListSortDirection
  38.             Get
  39.                 Return m_SortDirection
  40.             End Get
  41.         End Property
  42.  
  43.         Protected Overrides ReadOnly Property SortPropertyCore() As System.ComponentModel.PropertyDescriptor
  44.             Get
  45.                 Return m_SortProperty
  46.             End Get
  47.         End Property
  48.  
  49.         Protected Overrides ReadOnly Property IsSortedCore() As Boolean
  50.             Get
  51.                 Return m_SortProperty Is Nothing
  52.             End Get
  53.         End Property
  54.  
  55.         Private Sub SaveList()
  56.             m_OriginalList = New ArrayList(Me.Items)
  57.         End Sub
  58.  
  59.         Private Sub ResetList(ByVal NewList As ArrayList)
  60.             Me.ClearItems()
  61.             For Each m_Student As BaseData In NewList
  62.                 Me.Add(m_Student)
  63.             Next
  64.         End Sub
  65.  
  66.         Private Sub DoSort()
  67.             Dim m_Comparer As New BaseDataComparer(m_SortProperty, m_SortDirection)
  68.             Dim m_SortList As New ArrayList(Me.Items)
  69.             m_SortList.Sort(m_Comparer)
  70.             ResetList(m_SortList)
  71.         End Sub
  72.  
  73.         Protected Overrides Sub ApplySortCore(ByVal prop As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection)
  74.             m_SortProperty = prop
  75.             m_SortDirection = direction
  76.             If (m_OriginalList Is Nothing) Then
  77.                 SaveList()
  78.             End If
  79.             DoSort()
  80.         End Sub
  81.  
  82.         Protected Overrides Sub RemoveSortCore()
  83.             ResetList(m_OriginalList)
  84.             m_SortDirection = Nothing
  85.             m_SortProperty = Nothing
  86.         End Sub
  87. #End Region
  88.  
  89.  
  90.  
  91.     End Class
  92.  
  93.  
Here is an example of a derived object that inherits BaseData:

Expand|Select|Wrap|Line Numbers
  1.  Public Class MyUser
  2.         Inherits BaseData
  3.  
  4.         Dim msUserName As String
  5.         Dim msPassword As String
  6.  
  7.  
  8.         Public Property UserName As String
  9.             Get
  10.                 Return msUserName
  11.             End Get
  12.             Set(ByVal value As String)
  13.                 msUserName = value
  14.             End Set
  15.         End Property
  16.  
  17.  
  18.         Public Property Password As String
  19.             Get
  20.                 Return msPassword
  21.             End Get
  22.             Set(ByVal value As String)
  23.                 msPassword = value
  24.             End Set
  25.         End Property
  26.  
  27.  
  28.  
  29.         Public Overrides Function GetAllObjects() As Collections.BaseDataCollection
  30.             Dim GetParams As New stcReadFileParams
  31.             Dim myProcessor As New MasterProcessor
  32.             Dim sObjectRec As String = ""
  33.             Dim colObject As Collections.MyUserCollection = Nothing
  34.  
  35.             GetParams.FileName = FileName
  36.             GetParams.KeyWord = RN_USERS
  37.             GetParams.Mode = MODE_INQUIRY
  38.  
  39.             If Not UVSession Is Nothing Then myProcessor.UVSession = UVSession
  40.  
  41.             sObjectRec = myProcessor.GetMaster(GetParams)
  42.  
  43.             If sObjectRec <> "" Then
  44.                 colObject = ParseRecordIntoObjects(sObjectRec)
  45.             End If
  46.  
  47.             Return colObject
  48.         End Function
  49.  
  50.  
  51.     End Class
  52.  

Here is an example of a derived collection:

Expand|Select|Wrap|Line Numbers
  1.     Public Class MyUserCollection
  2.         Inherits BaseDataCollection
  3.  
  4.     End Class
  5.  
When I try to databind the result from MyUser.GetAllObjects to my gridView I am only seeing the BASEDATA object properties of DataID and Description. What am I doing wrong? I thought that the rules of polymorphism would strongly type the BindingList (of BaseData) in to BindingList (of MyUser) but that does not appear to be the case.

Any help would be greatly appreciated.
Jan 18 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.