-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Custom List
PostPosted: Mon Jun 26, 2006 1:18 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
How do I pass the IList that is from database (from the Load() method) to a custom collection class. Something like...
Code:
/*
* SectionCollection.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Collections;

namespace NBulletin.Core
{
   /// <summary>
   /// Summary description for SectionCollection.
   /// </summary>
   public class SectionCollection : CollectionBase
   {
      public SectionCollection() {}

      public void Add(Section s)
      {
         this.List.Add(s);
      }

      public Section this[int i]
      {
         get { return (Section)this.List[i]; }
         set { this.List[i] = value; }
      }

      public IList NewsAndFeatures
      {
         get { return GetSections(Category.NewsAndFeatures); }
      }

      public IList KababayanEditions
      {
         get { return GetSections(Category.KababayanEdition); }
      }

      public IList Hometowns
      {
         get { return GetSections(Category.Hometown); }
      }

      public IList OwningAHome
      {
         get { return GetSections(Category.OwningAHome); }
      }

      private IList GetSections(Category category)
      {
         IList list = new ArrayList();
         foreach (Section s in this.List)
            if (s.Category == category)
               list.Add(s);
         return list;
      }
   }
}

Any help? Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 6:08 am 
Newbie

Joined: Mon Jun 05, 2006 8:51 am
Posts: 18
Were either InnerList or List properties of CollectionBase virtual, it would be pretty straightforward. Unfortunatelly, this is not the case. Nevertheless it is still doable. Essentially, you have to write a typed IList-wrapper, which delegates all calls to a wrapped list. Here's how I do that (please note explicit interface implementations):

Code:
namespace CommonServer.ObjectModel.Business.Collections
{
   public abstract class ListWrapperBase : IList
   {
      #region Private Member Variables
      private IList wrappedList;
      #endregion

      #region Protected Properties
      protected IList WrappedList
      {
         get { return wrappedList; }
      }
      #endregion

      public ListWrapperBase(IList wrappedList)
      {
         this.wrappedList = wrappedList;
      }

      #region IList Members
      public bool IsReadOnly
      {
         get { return wrappedList.IsReadOnly; }
      }

      public bool IsFixedSize
      {
         get { return wrappedList.IsFixedSize; }
      }

      int IList.Add(object value)
      {
         return wrappedList.Add(value);
      }

      bool IList.Contains(object value)
      {
         return wrappedList.Contains(value);
      }

      public void Clear()
      {
         wrappedList.Clear();
      }

      int IList.IndexOf(object value)
      {
         return wrappedList.IndexOf(value);
      }

      void IList.Insert(int index, object value)
      {
         wrappedList.Insert(index, value);
      }

      void IList.Remove(object value)
      {
         wrappedList.Remove(value);
      }

      public void RemoveAt(int index)
      {
         wrappedList.RemoveAt(index);
      }

      object IList.this[int index]
      {
         get { return wrappedList[index]; }
         set { wrappedList[index] = value; }
      }

      public int Count
      {
         get { return wrappedList.Count; }
      }

      public object SyncRoot
      {
         get { return wrappedList.SyncRoot; }
      }

      public bool IsSynchronized
      {
         get { return wrappedList.IsSynchronized; }
      }

      public void CopyTo(Array array, int index)
      {
         wrappedList.CopyTo(array, index);
      }

      public IEnumerator GetEnumerator()
      {
         return wrappedList.GetEnumerator();
      }
      #endregion
   }
}

And here's a typed ancestor:

Code:
using System.Collections;

using CommonServer.ObjectModel.Business.Objects;

namespace CommonServer.ObjectModel.Business.Collections
{
   public class CityListWrapper : ListWrapperBase
   {
      public City this[int index]
      {
         get { return WrappedList[index] as City; }
         set { WrappedList[index] = value; }
      }

      public CityListWrapper(IList wrappedList) :
         base(wrappedList)
      {
      }

      public int Add(City businessObject)
      {
         return WrappedList.Add(businessObject);
      }

      public bool Contains(City businessObject)
      {
         return WrappedList.Contains(businessObject);
      }

      public int IndexOf(City businessObject)
      {
         return WrappedList.IndexOf(businessObject);
      }

      public void Insert(int index, City businessObject)
      {
         WrappedList.Insert(index, businessObject);
      }

      public void Remove(City businessObject)
      {
         WrappedList.Remove(businessObject);
      }
   }
}

And finally business object, which contains a collection of City objects (Country in my case)

Code:
public class Country
{
        private IList cities = new ArrayList();

        // Yes, creating a new instance of CityListWrapper is performance penalty
        public CityListWrapper Cities
        { get { return new CityListWrapper(cities); } }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.