none
WPF C# issue RRS feed

  • Question

  • I am having an issue with a for each in WPF trying to get it to out put what am i doing wrong
    Form1.cs
    
    
    namespace boxes
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                foreach green = new foreach();
                
                
     		textBox4.Text = green.person;
    
    
                }
    
    
         }
    }
    
    
    
    
    looparound.cs
    
    
    
        class foreach
        {
    
        
            public string person { get; set; }
            
            public void round()
            {
                List<string> demo = new List<string>();
    
                demo.Add("Bob");
                demo.Add("George");
                demo.Add("Tom");
                demo.Add("Sue");
    
                foreach (string person in demo)
                {
                    person.ToString();
                }
    
    
               
            }
        }
    }

    • Moved by Luigi BrunoMVP Friday, April 5, 2019 7:48 PM Question related to the C# programming language.
    Friday, April 5, 2019 7:12 PM

All replies

  • I'd try asking for help over here.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=windowsforms

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Friday, April 5, 2019 7:36 PM
  • This is a Winforms form, not WPF.

    I assume this code doesn't even compile correct? `foreach` is a reserved word. You cannot declare a class (or any identifier) with that name. That is the first issue. Types represent data so what is your type representing? Based upon the look of it, just a person but we'll assume an employee. Let's fix the type first.

    //looparound.cs
    class Employee
    {
       public string Person { get; set; }
    
       public void Round ()
       {
          List<string> demo = new List<string>();
          demo.Add("Bob");
          demo.Add("George");
          demo.Add("Tom");
          demo.Add("Sue");
    
          //Enumerate the demo list
          foreach (string person in demo)
          {
             person.ToString();
          }
       }
    }

    Some subtle changes here. `foreach` type (which doesn't compile) becomes a Pascal cased noun representing what the type is, `Employee` in this case. We use Pascal casing for types and public members to avoid conflicts. C# is case sensitive.

    The `person` property is fine but we make it Pascal cased to be consistent with the rest of .NET. 

    The `round` method we Pascal case as well. It doesn't seem to do anything but test a foreach loop so we left it unchanged. But notice inside that loop that you were declaring a `person` variable as part of the foreach loop. Since that variable had the same name as your `person` property it was hiding the definition. Since `Round` doesn't actually do anything with your type it is harmless here but would cause problems down the road. Inside the foreach you're simply getting the string equivalent of the string so it doesn't do anything useful. 

    Now to the form class you have. 

    private void Form1_Load(object sender, EventArgs e)
    {
       Employee green = new Employee();
    
       textBox4.Text = green.Person;
    }

    Again, `foreach` is a keyword so you cannot create an instance of it. We switch to your `Employee` type. We fix the casing on it and the `person` property and now the code will compile and run. When the form loads it will create an instance of your `Employee` class and then set textBox4's `Text` property to the employee's `Person` property. However since you didn't initialize it to anything the textbox will simply remain empty. We can fix that by setting the property to something first.

    private void Form1_Load(object sender, EventArgs e)
    {
       Employee green = new Employee();
       green.Person = "Bob";
    
       textBox4.Text = green.Person;
    }
    Now the textbox will be set to `Bob` when the form loads.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 5, 2019 9:46 PM
    Moderator
  • Hello,

    In the code sample below I changed the name of the class as that name is unwise.

    public class Class1
    {
        public string person
        {
            get { return round(); }
        }
    
        public string round()
        {
            var demo = new List<string> {"Bob", "George", "Tom", "Sue"};
            return string.Join(",", demo);
        }
    }

    Usage

    var demo = new Class1();
    textBox4.Text = demo.person;


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 5, 2019 9:50 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 9, 2019 2:58 AM
    Moderator