Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:50:24+00:00 2026-06-17T20:50:24+00:00

I put a generic list in a private helper method… private void addButton() {

  • 0

I put a generic list in a private helper method…

    private void addButton()
    {
        List<Button> buttonList = new List<Button>();

        buttonList.Add(button1);
        buttonList.Add(button2);
        buttonList.Add(button3);
        buttonList.Add(button4);
        buttonList.Add(button5);
        buttonList.Add(button6);
        buttonList.Add(button7);
        buttonList.Add(button8);
        buttonList.Add(button9); 
    }

I know to call this method you have to do something like this…

    private void button1_Click(object sender, EventArgs e)
    {
            addButton();

            foreach (button in addButton())
            {
                button.Enabled = false;
            }
    }

however… when I try to call button in addButton() red lines appear underneath ‘button’ saying that it could not be found. Please can someone help 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:50:25+00:00Added an answer on June 17, 2026 at 8:50 pm

    Your method should return the list:

    private List<Button> addButton()
    {
        List<Button> buttonList = new List<Button>();
    
        buttonList.Add(button1);
        //...
        return buttonList;
    }
    

    Also you don’t need the first addButton();:

    private void button1_Click(object sender, EventArgs e)
    {
            foreach (Button button in addButton())
            {
                button.Enabled = false;
            }
    }
    

    And I would have this method rewritten with yield keyword:

    private IEnumerable<Button> addButton()
    {
        yield return button1;
        yield return button2;
        //...
        yield return button9;
    }
    

    as you do not need to create a list of buttons just to go through them each time you need to. It’s enough just to enumerate them.


    Also review Methods (C# Programming Guide), section about methods returning values:

    Methods can return a value to the caller. If the return type, the type
    listed before the method name, is not void, then the method can return
    the value using the return keyword. A statement with the keyword
    return followed by a value that matches the return type will return
    that value to the method caller. The return keyword also stops the
    execution of the method. If the return type is void, a return
    statement with no value is still useful to stop the execution of the
    method. Without the return keyword, the method will stop executing
    when it reaches the end of the code block. Methods with a non-void
    return type are required to use the return keyword to return a value.


    P.S.: review my answer in How do I shorten this generic list?. Probably this would still be a better approach?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function: private void button6_Click(object sender, EventArgs e) { crawlLocaly1 = new
We have Generics Property public class BE { private List<Admin_Fee> _Admin_Fee = new List<Admin_Fee>();
I am trying to send a generic list to a jersey servlet using put
In the following code: private static void Main(string[] args) { var listy = new
I want to put temporary datas into a generic list and bind it to
I'm attempting to put together a generic method invoker (for a C++ OO/v8 bridge),
I'm not really familiar with creating generic methods, so I thought I'd put this
How can I put the type of a variable in a method parameter as
I have such generic class of list: using System; using System.Collections; using System.Collections.Generic; using
Can I serialize a generic list of serializable objects without having to specify their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.