Bob
Bob

Reputation: 1375

Populate an array with items from a List of objects using a loop

I have a list of type Product. . .

public List<Product> products = new List<Product>();

. . . and I want to create a method GetList(string theType) that will populate an array with items from that List if the theType argument supplied with the method matches the Type field inside any of the objects in the List.

Only thing I want the array to contain when it is returned are the names of all the products that were matched successfully against the supplied theType argument.

   public string[] GetList(string theType)
        {
            string[] theList = new string[10];
            for(int i = 0; i < theList.Length; i++)
            {
                foreach (Product p in products)
                {
                    if (p.Type.Equals(theType))
                    {
                        theList[i] = p.ProductName; 
                    }
                }
            }
            return theList;
        }

This doesn't seem to work. Even I can see it. I am just too tired to think this out.

EDIT:

I want to populate a combobox with the returned theList. There are two comboboxes. You have to select a preset value in the first one in order to enable and second one which should be populated with products items of type that was selected in combobox1. I only have one event handling for combobox1:

    private void combobox1_SelectedValueChanged(object sender, EventArgs e)
            {
                if (combobox1.Text != "")
                {
                    combobox2.Enabled = true;
                    combobox2.Items.Clear();

                    if (combobox1.SelectedText.Equals("Dairy"))
                    {
// i try to display what the method has returned inside a messagebox, but it doesn't display it at all, the messagebox
                        string[] theList = client.GetList("dairy");
                        string theStringList = "";

                        for (int i = 0; i < theList.Length; i++)
                        {
                            theStringList += "\n" + theList[i];
                        }
                        MessageBox.Show(String.Format("{0}"), theStringList);
                        //combobox2.Items.AddRange(client.GetList("dairy"));
                    }
                }
                else
                    combobox2.Enabled = false;
            }

Upvotes: 1

Views: 5610

Answers (4)

user1200540
user1200540

Reputation:

why are you putting it in a for loop?

public List<String> GetList(string theType)
    {
        List<String> TheList = new List<String>();

            foreach (Product p in products)
            {
                if (p.Type = theType))
                {
                    theList.add(ProductName); 
                }
            }

        return theList;
    }

Then for your edit (populating a combo box)

you should use a foreach loop after getting the list

List<String> iList = GetList("typenamehere")

    foreach(string s in theList){
    Combobox1.Add(s);
    }

then on selection change you can check the combobox.selecteditem.text

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160892

Since you don't know how many items will match, use a List<string> rather than an array:

public IList<string> GetList(string theType)
{
    List<string> matchingProductNames = new List<string>();
    foreach (Product p in products)
    {
        if (p.Type == theType)
           matchingProductNames.Add(p.ProductName);
    }
    return matchingProductNames;
}

This alternatively also can be done in a much more expressive way (in my opinion) with Linq:

string[] productNames = products.Where(p => p.Type == theType)
                                .Select(p => p.ProductName)
                                .ToArray();

You currently display the text content as caption of your message box since it is the second parameter to MessageBox.Show - since you first character is a newline though you will not see anything:

MessageBox.Show(String.Format("{0}"), theStringList);

You probably meant to write MessageBox.Show(string.Format("{0}", theStringList)) but there is no point in using string.Format here in the first place since you don't apply formatting, just use the string directly:

string theStringList = string.Join("\n", client.GetList("dairy"));
MessageBox.Show(theStringList, "some caption");

Upvotes: 1

Balazs Tihanyi
Balazs Tihanyi

Reputation: 6719

With LINQ:

return products.Where(p => p.Type == theType).Select(p => p.ProductName).ToArray();

Upvotes: 6

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

public string[] GetList(String theType)
    {
        ArrayList theList = new ArrayList();
        foreach (Product p in products)
        {
            if (p.GetType().ToString() == theType)
                theList.Add(p.ProductName);
        }
        return theList.Cast<string>().ToArray();
    }

Upvotes: 1

Related Questions