GetIdValue and Lists

GetIdValue and Lists

Old forum URL: forums.lhotka.net/forums/t/6287.aspx


Wbmstrmjb posted on Thursday, January 29, 2009

It seems to me that any list would want a "GetById" function to pull specific objects out of the list based on their Id.  I was hoping to add this to my inherited list bases since it is not available by default, but GetIdValue is protected.  Is it possible to create a GetById function that returns an object based on that object's GetIdValue in the list bases?

Example: 

   MyBusinessBase<T> : Csla.BusinessBase<T>

   MyBusinessListBase<T, C> : Csla.BusinessListBase<T, C> where T : MyBusinessListBase<T, C> where C : MyBusinessBase<C>

   In MyBusinessListBase, I want:

   public C GetByID(object id)

   {

      foreach (C item in this)

      {

         if  (item.GetIdValue() == id)

            return item;

      }

   }

 

Thanks,

Mike

JoeFallon1 replied on Thursday, January 29, 2009

You could always create an Interface and include that method.

e.g.

Public Interface IMyBusinessObject
  Inherits IEditableBusinessObject
  Function GetIdValue() As Object
End Interface

Then you could cast each item to the interface and call GetIdValue.

Public Overloads ReadOnly Property GetById(ByVal id As Object) As C
  Get
    Dim obj As IMyBusinessObject
    For Each obj In Me
      If obj.GetIdValue.Equals(id) Then
        Return DirectCast(obj, C)
      End If
    Next
    Return Nothing
  End Get
End Property

Joe

Copyright (c) Marimer LLC