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....

2
  • 2
    You'll need to return a collection, like a list. You can also return a dynamically generated collection of type IEnumerable<T> with the help of the yield return construct. Commented Apr 16, 2015 at 19:00
  • yield return is what you want. Commented Apr 16, 2015 at 19:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can you elaborate a bit more on how to use IEnumerables exactly?
@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?
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

5 Comments

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?
did you set the return type of the method to IEnumerable<string>? Maybe you can post the whole method to ease up support.
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.
I really don't know what an IEnumberable string is...?
added a link in my post to some IEnumerable basics

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.