The Mask
The Mask

Reputation: 17427

how to implement Collection object

how to implement collection objects of my class?

something like MatchCollection or CookieCollection

For example:

I have the following class:

public class theParserClass 
{
    public theParserClass(string baa)
    { 
      //..
    }

    public string pro1 
   {
     get { /* etc */ } 
   }
}

and the collection that I want to implement:

 public class theParserClassResultCollection
    {
       private ParserResultCollection result; 
       public theParserClassResultCollection(string[] baa)
       { 

         foreach(string foo in baa)
         {
           var data = new theParserClass(foo);
           result.Add(data);
         }
       }

       public ParserResultCollection() 
       {
        return result;            
       }

    }

I hope this is clear. Thanks in advance

Upvotes: 0

Views: 184

Answers (3)

WadeJohnston
WadeJohnston

Reputation: 13

You could try this:

  public class Collection<T> : IList<T>, 
        ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

http://msdn.microsoft.com/en-us/library/ms132397.aspx

Upvotes: 0

punker76
punker76

Reputation: 14601

you can use the ObservableCollection like this:

public ObservableCollection<ParserClass> GetCollection(string[] baa)
{
  var result = new ObservableCollection<ParserClass>();
  foreach(string foo in baa)
  {
    var data = new ParserClass(foo);
    result.Add(data);
  }
  return result;
}

public class ParserClass 
{
  public ParserClass (string baa)
  { 
    //..
  }

  public string pro1 
  {
    get { /* etc */ } 
  }
}

msdn : http://msdn.microsoft.com/en-us/library/ms668604.aspx

hope this helps

Upvotes: 1

Tudor
Tudor

Reputation: 62439

First off, you are declaring what appears to be the constructor of ParserResultCollection inside the class theParserResultCollection. Don't really know what that is supposed to mean.

The general idea you can use is to make a wrapper class over an existing collection (inheritance by composition) and provide the methods that you need using the inner collection object. Like:

public class ParserResultCollection
{
    private List<ParserClass> collection;

    public ParserResultCollection(string[] param)
    {
        collection = new List<ParserClass>(param);            
    }

    public void Add(ParserClass item)
    {
        collection.Add(item);
    }

    // whatever else you need.
}

Of course, if you don't need any other special functionality with respect to the existing collections, just use them instead.

And drop the "the", just ParserResultCollection. It's cleaner.

Upvotes: 0

Related Questions