title: Populate an array with items from a List of objects using a loop
body: <p>I have a list of type <code>Product</code>. . .</p>
  
  <pre><code>public List&lt;Product&gt; products = new List&lt;Product&gt;();
  </code></pre>
  
  <p>. . . and I want to create a method <code>GetList(string theType)</code> that will populate an array with items from that <code>List</code> if the <code>theType</code> argument supplied with the method matches the <code>Type</code> field inside any of the objects in the <code>List</code>. </p>
  
  <p>Only thing I want the array to contain when it is returned are the <strong>names</strong> of all the products that were matched successfully against the supplied <code>theType</code> argument.</p>
  
  <pre><code>   public string[] GetList(string theType)
          {
              string[] theList = new string[10];
              for(int i = 0; i &lt; theList.Length; i++)
              {
                  foreach (Product p in products)
                  {
                      if (p.Type.Equals(theType))
                      {
                          theList[i] = p.ProductName; 
                      }
                  }
              }
              return theList;
          }
  </code></pre>
  
  <p>This doesn't seem to work. Even I can see it. I am just too tired to think this out.</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>I want to populate a combobox with the returned <code>theList</code>.
  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:</p>
  
  <pre><code>    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 &lt; theList.Length; i++)
                          {
                              theStringList += "\n" + theList[i];
                          }
                          MessageBox.Show(String.Format("{0}"), theStringList);
                          //combobox2.Items.AddRange(client.GetList("dairy"));
                      }
                  }
                  else
                      combobox2.Enabled = false;
              }
  </code></pre>