0

I'm working on a program right now, and I was wondering if it were possible to have a return function that will return the object/value/variable generated during every loop? Below is the code that I want to work. My only error is the return values.

for (int i = 1; i < ProductArray.Length; i++)
{
    Label lbl = new Label();
    ThresholdPanel.Controls.Add(lbl);
    lbl.Top = A * 28;
    lbl.Left = 15;
    lbl.Font = new Font(lbl.Font, FontStyle.Bold);
    lbl.Text = ProductArray[i];
    lbl.Name = "Label" + ProductArray[i];

    TextBox txt = new TextBox();
    ThresholdPanel.Controls.Add(txt);
    txt.Top = A * 28;
    txt.Left = 125;
    //txt.Text = "Text Box All" + this.A.ToString();
    txt.Name = "txt" + A;
    textBoxes[txt.Name] = txt;
    A = A + 1;

    return txt;
    return lbl;
} 

Thanks in advance and I'm sorry if this is really a simple question....

Leonardo
  • 10,737
  • 10
  • 62
  • 155
AndrewD
  • 75
  • 7

2 Answers2

4

use yield return instead of return as long as the method returns an IEnumerable<T> where T is the type that you're wanting to yield. It will result in a method that returns a sequence of items, and adds an item to that sequence for every item you yield return.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • can you elaborate a bit more on how to use IEnumerables exactly? – AndrewD Apr 16 '15 at 19:25
  • @AndrewDahdouh If you're just looking for information about it in general, the place to start would be the documentation. Do you have a specific question about it? – Servy Apr 16 '15 at 19:31
2

Use yield return as in provided sample:

IEnumerable<string> Test()
{
    for (int i = 1; i < ProductArray.Length; i++)
    {
        Label lbl = new Label();
        ThresholdPanel.Controls.Add(lbl);
        lbl.Top = A * 28;
        lbl.Left = 15;
        lbl.Font = new Font(lbl.Font, FontStyle.Bold);
        lbl.Text = ProductArray[i];
        lbl.Name = "Label" + ProductArray[i];

        TextBox txt = new TextBox();
        ThresholdPanel.Controls.Add(txt);
        txt.Top = A * 28;
        txt.Left = 125;
        //txt.Text = "Text Box All" + this.A.ToString();
        txt.Name = "txt" + A;
        textBoxes[txt.Name] = txt;
        A = A + 1;

        yield return txt;
    }
}

More details on IEnumerable

Community
  • 1
  • 1
Michael Sander
  • 2,677
  • 23
  • 29
  • I tried to change it to say yield return....which got rid of some errors, but caused another. It's saying the body of the method cannot be an iterator block because 'object' is an iterator interface type? – AndrewD Apr 16 '15 at 19:17
  • did you set the return type of the method to IEnumerable? Maybe you can post the whole method to ease up support. – Michael Sander Apr 16 '15 at 19:22
  • I tried to change it to say yield return....which got rid of some errors, but caused another. It's saying the body of the method cannot be an iterator block because 'object' is an iterator interface type? The method that this for loop is defined in is declared as a public object. – AndrewD Apr 16 '15 at 19:22
  • I really don't know what an IEnumberable string is...? – AndrewD Apr 16 '15 at 19:23
  • added a link in my post to some IEnumerable basics – Michael Sander Apr 16 '15 at 19:27