Collections in C#


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Collections in C#

Hybrid View

  1. #1
    Ricardo Guest

    Collections in C#


    I'm trying to create a mechanism of sales. I have a Sales class and an Item
    class. The Sales contains a lot of items. So, how can I make it?
    If I use an array (public Item[] item) in the Sales class, I have to give
    an inicial size for the array and this is bad (I don't know how many items
    I will have).


  2. #2
    Jay Glynn Guest

    Re: Collections in C#

    You can either look at using ArrayList, it's dynamic and can grow as needed
    or you can create your own collection class. If that is what you wish look
    at using CollectionBase as your base class.

    "Ricardo" <rmendes@aksistemas.com.br> wrote in message
    news:3d469a9e$1@10.1.10.29...
    >
    > I'm trying to create a mechanism of sales. I have a Sales class and an

    Item
    > class. The Sales contains a lot of items. So, how can I make it?
    > If I use an array (public Item[] item) in the Sales class, I have to give
    > an inicial size for the array and this is bad (I don't know how many items
    > I will have).
    >




  3. #3
    Simon Franklin Guest

    Re: Collections in C#


    "Jay Glynn" <jlsglynn@hotmail.com> wrote:
    >You can either look at using ArrayList, it's dynamic and can grow as needed
    >or you can create your own collection class. If that is what you wish look
    >at using CollectionBase as your base class.
    >
    >"Ricardo" <rmendes@aksistemas.com.br> wrote in message
    >news:3d469a9e$1@10.1.10.29...
    >>
    >> I'm trying to create a mechanism of sales. I have a Sales class and an

    >Item
    >> class. The Sales contains a lot of items. So, how can I make it?
    >> If I use an array (public Item[] item) in the Sales class, I have to give
    >> an inicial size for the array and this is bad (I don't know how many items
    >> I will have).
    >>

    >
    >

    The following two classes show how to create a collection class that holds
    a list of Authors modify the code to your own design and enjoy.

    /// <summary>
    /// Summary description for the Author class.
    /// </summary>
    [Serializable]
    public class Author
    {
    /// <summary>
    /// Summary description for private String authorid;.
    /// </summary>
    private String authorid;
    /// <summary>
    /// Summary description for private String firstname;.
    /// </summary>
    private String firstname;
    /// <summary>
    /// Summary description for private String lastname;.
    /// </summary>
    private String lastname;
    /// <summary>
    /// Summary description for Property AuthorID .
    /// </summary>
    public String AuthorID
    {
    get
    {
    return authorid;
    }
    set
    {
    authorid = value;
    }
    }
    /// <summary>
    /// Summary description for Property FirstName .
    /// </summary>
    public String FirstName
    {
    get
    {
    return firstname;
    }
    set
    {
    firstname = value;
    }
    }
    /// <summary>
    /// Summary description for Property LastName .
    /// </summary>
    public String LastName
    {
    get
    {
    return lastname;
    }
    set
    {
    lastname = value;
    }
    }
    /// <summary>
    /// Summary description for Author().
    /// </summary>
    public Author()
    {

    }
    /// <summary>
    /// Summary description for Author(String authorid,String firstname,String
    lastname,AuthorAddress address).
    /// </summary>
    public Author(String authorid,String firstname,String lastname)
    {
    this.AuthorID = authorid;
    this.FirstName = firstname;
    this.LastName = lastname;
    }
    /// <summary>
    /// Summary description for Author(Author AuthorDetails).
    /// </summary>
    public Author(Author AuthorDetails)
    {
    this.AuthorID = AuthorDetails.AuthorID;
    this.FirstName = AuthorDetails.FirstName;
    this.LastName = AuthorDetails.LastName;
    }
    }
    /// <summary>
    /// Summary description for public class AuthorsCollection : System.Collections.CollectionBase
    /// </summary>
    [Serializable]
    public class AuthorsCollection : System.Collections.CollectionBase
    {
    /// <summary>
    /// Summary description for public Author this[int index].
    /// </summary>
    public Author this[int index]

    {
    get
    {
    return (Author) List[index];
    }
    set
    {
    List[index] = value;
    }
    }
    /// <summary>
    /// Summary description for public Author item(int index).
    /// </summary>
    public Author item(int index)

    {
    return (Author) List[index];
    }

    /// <summary>
    /// Summary description for public void Add(AssociateContentItem ContentItem).
    /// </summary>
    public void Add(Author AuthorItem)
    {
    List.Add(AuthorItem);
    }

    /// <summary>
    /// Summary description for public void Remove(int index).
    /// </summary>
    public void Remove(int index)
    {
    if (index > Count - 1 || index < 0)
    {
    ///
    }
    else
    {
    List.RemoveAt(index);
    }
    }
    /// <summary>
    /// Summary description for Add constructor logic.
    /// </summary>
    public AuthorsCollection()
    {

    }

    protected override void OnInsert(int index,object value)
    {

    }
    }

  4. #4
    Simon Franklin Guest

    Re: Collections in C#


    "Simon Franklin" <simonfranklin@iperium.com> wrote:
    >
    >"Jay Glynn" <jlsglynn@hotmail.com> wrote:
    >>You can either look at using ArrayList, it's dynamic and can grow as needed
    >>or you can create your own collection class. If that is what you wish look
    >>at using CollectionBase as your base class.
    >>
    >>"Ricardo" <rmendes@aksistemas.com.br> wrote in message
    >>news:3d469a9e$1@10.1.10.29...
    >>>
    >>> I'm trying to create a mechanism of sales. I have a Sales class and an

    >>Item
    >>> class. The Sales contains a lot of items. So, how can I make it?
    >>> If I use an array (public Item[] item) in the Sales class, I have to

    give
    >>> an inicial size for the array and this is bad (I don't know how many

    items
    >>> I will have).
    >>>

    >>
    >>

    >The following two classes show how to create a collection class that holds
    >a list of Authors modify the code to your own design and enjoy.
    >
    >/// <summary>
    > /// Summary description for the Author class.
    > /// </summary>
    > [Serializable]
    > public class Author
    > {
    > /// <summary>
    > /// Summary description for private String authorid;.
    > /// </summary>
    > private String authorid;
    > /// <summary>
    > /// Summary description for private String firstname;.
    > /// </summary>
    > private String firstname;
    > /// <summary>
    > /// Summary description for private String lastname;.
    > /// </summary>
    > private String lastname;
    > /// <summary>
    > /// Summary description for Property AuthorID .
    > /// </summary>
    > public String AuthorID
    > {
    > get
    > {
    > return authorid;
    > }
    > set
    > {
    > authorid = value;
    > }
    > }
    > /// <summary>
    > /// Summary description for Property FirstName .
    > /// </summary>
    > public String FirstName
    > {
    > get
    > {
    > return firstname;
    > }
    > set
    > {
    > firstname = value;
    > }
    > }
    > /// <summary>
    > /// Summary description for Property LastName .
    > /// </summary>
    > public String LastName
    > {
    > get
    > {
    > return lastname;
    > }
    > set
    > {
    > lastname = value;
    > }
    > }
    > /// <summary>
    > /// Summary description for Author().
    > /// </summary>
    > public Author()
    > {
    >
    > }
    > /// <summary>
    > /// Summary description for Author(String authorid,String firstname,String
    >lastname,AuthorAddress address).
    > /// </summary>
    > public Author(String authorid,String firstname,String lastname)
    > {
    > this.AuthorID = authorid;
    > this.FirstName = firstname;
    > this.LastName = lastname;
    > }
    > /// <summary>
    > /// Summary description for Author(Author AuthorDetails).
    > /// </summary>
    > public Author(Author AuthorDetails)
    > {
    > this.AuthorID = AuthorDetails.AuthorID;
    > this.FirstName = AuthorDetails.FirstName;
    > this.LastName = AuthorDetails.LastName;
    > }
    > }
    >/// <summary>
    > /// Summary description for public class AuthorsCollection : System.Collections.CollectionBase
    > /// </summary>
    > [Serializable]
    > public class AuthorsCollection : System.Collections.CollectionBase
    > {
    > /// <summary>
    > /// Summary description for public Author this[int index].
    > /// </summary>
    > public Author this[int index]
    >
    > {
    > get
    > {
    > return (Author) List[index];
    > }
    > set
    > {
    > List[index] = value;
    > }
    > }
    > /// <summary>
    > /// Summary description for public Author item(int index).
    > /// </summary>
    > public Author item(int index)
    >
    > {
    > return (Author) List[index];
    > }
    >
    > /// <summary>
    > /// Summary description for public void Add(AssociateContentItem ContentItem).
    > /// </summary>
    > public void Add(Author AuthorItem)
    > {
    > List.Add(AuthorItem);
    > }
    >
    > /// <summary>
    > /// Summary description for public void Remove(int index).
    > /// </summary>
    > public void Remove(int index)
    > {
    > if (index > Count - 1 || index < 0)
    > {
    > ///
    > }
    > else
    > {
    > List.RemoveAt(index);
    > }
    > }
    > /// <summary>
    > /// Summary description for Add constructor logic.
    > /// </summary>
    > public AuthorsCollection()
    > {
    >
    > }
    >
    > protected override void OnInsert(int index,object value)
    > {
    >
    > }
    > }

    //Sorry forgot to show the code to implement the two classes.
    //Create the AuthorsCollection class to hold the list of Authors.
    AuthorsCollection AC = new AuthorsCollection();
    //Create the Author class and set its properties.
    Author BookAuthor = new Author();
    BookAuthor.FirstName = "YourAuthorFirstName";
    BookAuthor.LastName = "YourAuthorLastName";
    BookAuthor.AuthorID = "YourAuthorID";
    //Add the Author class to the collection
    AC.Add(BookAuthor);
    //Now convert a member of the collection to an XmlElement and add it to an
    XmlDocument.
    XmlDocument xDoc = new XmlDocument();
    xDoc.LoadXml("<Authors></Authors>");
    XmlNode xNode = xDoc.DocumentElement;
    XmlElement MyAuthorElement = xDoc.CreateElement("Author");
    //Access the AuthorsCollection by index value.
    MyAuthorElement.SetAttribute("FirstName",AC.item(0).FirstName);
    MyAuthorElement.SetAttribute("LastName",AC.item(0).LastName);
    MyAuthorElement.SetAttribute("AuthorID",AC.item(0).AuthorID);
    //Add the new Element to the XmlDocument;
    xNode.AppendChild((XmlNode)MyAuthorElement);
    //The output should look something like the following;
    //<Authors>.
    // <Author FirstName="YourAuthorFirstName" LastName="YourAuthorLastName"
    AuthorID="YourAuthorID" />
    //</Authors>.
    //Also remember that because the AuthorsCollection is derived from System.Collections.CollectionBase
    //it can be accessed using foreach.
    //For example
    foreach (Author MyAuthor in AC)
    {
    Console.WriteLine(MyAuthor.FirstName);
    }

  5. #5
    David Chu Guest

    Re: Collections in C#


    How about to add a key feature to a collection so that each item can be added
    to the collection by a key, or item in the collection can be referenced by
    a key?

    Example:

    AC.Add ( authore, "key1");
    string name = AC.Item("Key1").FirstName;

    David Chu

  6. #6
    Jay Glynn Guest

    Re: Collections in C#

    Then you would want to use DictionaryBase or implement IDictionary.


    "David Chu" <chudq@hotmail.com> wrote in message
    news:3d6265b3$1@10.1.10.29...
    >
    > How about to add a key feature to a collection so that each item can be

    added
    > to the collection by a key, or item in the collection can be referenced by
    > a key?
    >
    > Example:
    >
    > AC.Add ( authore, "key1");
    > string name = AC.Item("Key1").FirstName;
    >
    > David Chu




  7. #7
    David Chu Guest

    Re: Collections in C#


    I tried to build a collection based on DictionaryBase. The attachment is the
    code for the class and test codes.

    It works fine except some points:

    1. It does not support index such as:
    Widget w = wCol1.Item(0);
    You have to use key to get item:
    Widget w = wCol1.Item("1");
    2. The order of retrieving items from collection in foreach loop is not the
    order of items added. For example, I added widget object in the order with
    Name properties of "One", "Two" and "Three". However, I got the result from
    foreach loop is widget with Names of "Two", "Three" and "Two".

    I don't understand the order of items in DictionaryBase collection.


    // ======================== Class definition, collection base on Widget class

    public class WidgetCollectionWKey : System.Collections.DictionaryBase
    {
    // Restricts to Widget types, items that can be added to the collection

    public void Add(object key, Widget aWidget )
    {
    Dictionary.Add(key, aWidget );
    }

    public void Remove(object key)
    {
    // Check to see if there is a widget at the supplied index.
    if ( key != null )
    {
    // If the DictionaryBase does not contain an element with the specified
    key,
    // the DictionaryBase remains unchanged. No exception is thrown.
    Dictionary.Remove( key );
    }
    }

    public Widget Item(object key)
    {
    // The appropriate item is retrieved from the List object and
    // explicitly cast to the Widget type, then returned to the
    // caller.
    return (Widget) Dictionary[ key ];
    }

    public bool ItemExit ( object key )
    {
    bool bRet = true;

    try
    {
    bRet = ( Dictionary.Contains( key ) );
    }
    catch { bRet = false; }
    return bRet;
    }

    }

    // ============ Testing codes:

    WidgetCollectionWKey wCol1 = new WidgetCollectionWKey();

    wCol1.Add ("2", new Widget("Two", "3") );
    wCol1.Add ("1", new Widget("One", "2") );
    wCol1.Add ("3", new Widget("Three", "1") );
    wCol1.Add ("4", null );

    foreach (DictionaryEntry d in wCol1 )
    {
    Widget w = (Widget) d.Value;
    if ( w != null )
    Console.WriteLine ("Item: {0}", w.Name );
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center