6 Replies - 2753 Views - Last Post: 18 July 2010 - 03:59 AM Rate Topic: -----

#1 Guest_Dustin*


Reputation:

Checking Data in ArrayList

Posted 17 July 2010 - 04:50 PM

I'm trying to check to see if the string entered by me already exists in the ArrayList, because there should not be any duplicates for the ID of a product. I keep getting that it doesn't exist, and allows me to re-enter the same ID multiple times. Here's my code:
ArrayList IDProduct = new ArrayList();

Product pct = new Product(); 

 if (IDProduct.Contains(txtID.Text)) /*i also tried it with object in the "contains" but realized it checks a  different one*/
 {
     MessageBox.Show("That ID already exists, Please enter a new ID");
 }
 else
 {

   pct.ID = txtID.Text;

   pct.ProductName = txtProductName.Text;
   pct.Descript = txtDescription.Text;
   
   MessageBox.Show("Product has been added");
   txtID.Text = "";
   txtProductName.Text = "";
   txtDescription.Text = "";


   IDProduct.Add(pct);
   }
 }



Is This A Good Question/Topic? 0

Replies To: Checking Data in ArrayList

#2 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: Checking Data in ArrayList

Posted 17 July 2010 - 05:10 PM

As you know already what type will be used(Product), it's better that you use List<type> instead of ArrayList.

Moreover, your if control will return always false because you're searching a string inside a Product container.

Here is a sample code to do what you need by using a List and a lambda expression.

List<Product> products = new List<Product>();
for (int i = 0; i < 10; ++i)
    products.Add(new Product() { Id = i });
            
// Searching for product with Id = 12
Product p = products.SingleOrDefault(k => k.Id == 12);

string message = (p != null) ? "found" : "not found";
Console.WriteLine(message);

Was This Post Helpful? 1
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Checking Data in ArrayList

Posted 17 July 2010 - 05:28 PM

Elegant and efficient, just wonderful. +1.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Dustin*


Reputation:

Re: Checking Data in ArrayList

Posted 17 July 2010 - 10:55 PM

I agree, however I'm already using the ArrayList throughout the rest of my program, and would have to do casting/converting if I did it that way, to the rest of my code. So then is there no way of searching for it the way I would like?
Was This Post Helpful? 0

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4464
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Checking Data in ArrayList

Posted 18 July 2010 - 12:11 AM

You should be able to do the same thing as JITHU suggested, but you'll have to cast k as a Product.
Was This Post Helpful? 0
  • +
  • -

#6 ParagonDevx  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 17
  • Joined: 09-April 10

Re: Checking Data in ArrayList

Posted 18 July 2010 - 03:10 AM

The thing is, your IDProduct ArrayList is empty; at least in the sample of code you're providing. Your "if condition" where you check for existing IDs will be false cause IDProduct contains nothing.

Is IDProduct a collection of Product ID's or a collection of Products?

When you're comparing items in an arrayList to any other object, unless the object compared to is of the same type and matches every property of one of the items in the ArrayList, ArrayList.Contains() will return false. In other words, if IDProduct does not have a string object that matches txtID.Text, "if (IDProduct.Contains(txtID.Text)) " will be false.

I don't know what kind of information you are putting into the ArrayList cause you didn't provide that piece of code. Correct me if I'm wrong but I suspect you're filling it up with Product objects, if that's the case, the condition where you check for existing product ID's will fail because you're trying to search for a string (txtID.Text) in a ArrayList of Products (two different kind of objects) by using ArrayList.Contains(). Previous posters already commented on this, I'm just expanding on it.

"So then is there no way of searching for it the way I would like? "

Of course there is, but since you're searching based on a string value (TxtID.Text) and you're using an ArrayList object, your ArrayList object (IDProduct) must contain string objects representing existing Product ID's ONLY. Make sure that what you're searching in the ArrayList is of the same type of the object you provide for the search.

Was This Post Helpful? 0
  • +
  • -

#7 Charles:)  Icon User is offline

  • D.I.C Regular

Reputation: 132
  • View blog
  • Posts: 321
  • Joined: 26-November 09

Re: Checking Data in ArrayList

Posted 18 July 2010 - 03:59 AM

View PostDustin, on 17 July 2010 - 09:55 PM, said:

I agree, however I'm already using the ArrayList throughout the rest of my program, and would have to do casting/converting if I did it that way, to the rest of my code. So then is there no way of searching for it the way I would like?


It really is worth putting in the time and effort to change your code so that it uses generic lists. ArrayLists are the old way of doing collections from .NET 1.0 & 1.1. Using generic lists will improve your program's performance and type safety and you'll be able to use LINQ queries on them since they implement the IEnumerable<T> interface.

Because generic lists are typed the compiler will be able to tell you where you're trying to do something wrong (such as your current problem of searching for a string in a list that actually contains products) so finding existing bugs and avoiding new bugs will also be much easier.

These are just some of the benefits but there are still more which I haven't mentioned, so I really do recommend switching to generic lists.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1