Collection.Add doesn't give me the expected result

Posted on 2005-04-18
Last Modified: 2010-04-16
Can't figure this one out.

I'm buildning a Collection inhertid from CollectionBase.

It seems that this code part doesn't work, when I use the collection in my WebForm:


public int Add(Company company)
  return this.InnerList.Add(company);


No matter what the Collection.Count remains zero - what's wrong??
Question by:korona5

    Author Comment

    Sorry made a mistake before...

    The problem lies in Collection.Contains here:

    _Company = new Company(1);

    _Company = new Company(1);
    _CompanyCollection.Contains(_Company) <--- this one returns false - I think it should return true - but why not???


    from collection:

                public bool Contains(BLL.Classes.Company company)
                      return this.InnerList.Contains(company);
    LVL 8

    Accepted Solution

    You have to implement IsEqual method in your company class

    Author Comment

    Thank you so much! I'm running late on this project :-)

    I did this:

    public override bool Equals(object obj)
          Company _Company = (obj as Company);

          if (_Company == null)
                return false;

          return this.CompanyId.Equals(_Company.CompanyId);

    And it works like a charm.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now