Why foreach(){} doesn't search in first part of a list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why foreach(){} doesn't search in first part of a list?

Here is a part of a code: List<Employee> parts = new List<Employee>(); parts.Add(new Employee()); Employee.Make(); parts.Add(new Employee()); Employee.Make(); void Edit() { Console.WriteLine("What do you want to edit? 1 - workers, 2 - Departments."); int answer = int.Parse(Console.ReadLine()); if (answer == 1) { Console.WriteLine("1 - Edit, 2 - Fire"); int workAnswer = int.Parse(Console.ReadLine()); if (workAnswer == 1) { Console.WriteLine("Which one? Enter id or fullname"); var editAnswer = Console.ReadLine(); foreach (Employee empl in parts) { if (editAnswer == Employee.id) { Console.WriteLine("-f

30th Aug 2019, 8:25 AM
Dead Lord
Dead Lord - avatar
13 Answers
+ 3
When you add to list "new Employee" - you already create a new copy , or most correct - instance of the class. But your class have a static variables which common for all instance of class and for all time will rewriting when you create a new instance, even if instance stored on list. So, you have 2 way - change variable and methods to non static , or use dataset for collect information. https://code.sololearn.com/cSvr3OhRYwcL/?ref=app
1st Sep 2019, 1:38 PM
id001x
id001x - avatar
1st Sep 2019, 2:05 PM
id001x
id001x - avatar
+ 2
It's bad read code. Write code in playground, and paste it in post.
30th Aug 2019, 8:49 AM
id001x
id001x - avatar
+ 2
ID001x thank you a lot!🤘
1st Sep 2019, 3:55 PM
Dead Lord
Dead Lord - avatar
+ 1
List<Employee> parts = new List<Employee>(); parts.Add(new Employee()); Employee.Make(); parts.Add(new Employee()); Employee.Make(); void Edit() { Console.WriteLine("What do you want to edit? 1 - workers, 2 - Departments."); int answer = int.Parse(Console.ReadLine()); if (answer == 1) { Console.WriteLine("1 - Edit, 2 - Fire"); int workAnswer = int.Parse(Console.ReadLine()); if (workAnswer == 1) { Console.WriteLine("Which one? Enter id or fullname"); var editAnswer = Console.ReadLine(); foreach (Employee empl in parts) { if (editAnswer == Employee.id) { Console.WriteLine("-found-");
30th Aug 2019, 8:25 AM
Dead Lord
Dead Lord - avatar
+ 1
It writes "-found-" only if I write secont part of my list info
30th Aug 2019, 8:27 AM
Dead Lord
Dead Lord - avatar
30th Aug 2019, 9:19 AM
Dead Lord
Dead Lord - avatar
+ 1
ID001x it can be too long. But still maybe you'll fave some advices for me :)
30th Aug 2019, 9:20 AM
Dead Lord
Dead Lord - avatar
+ 1
ID001x like it skip {0} and starts with {1}. It skips first position. That's better description I hope :)
30th Aug 2019, 10:29 AM
Dead Lord
Dead Lord - avatar
+ 1
Variable id in Employee class is static . It's means that it variable will be common for all instance of the class Employee. So, when you add second employ id in first employ will rewrite on id of second employ. You must change type to non static.
30th Aug 2019, 10:45 AM
id001x
id001x - avatar
+ 1
ID001x but it creates a new copy of Emloyee class every time i do new list. Or I get something wrong? :( Can you give me some advice how can I create list of classes and use any information in those classes? Search, combine, rename, delete and etc.
1st Sep 2019, 1:27 PM
Dead Lord
Dead Lord - avatar
0
What do you means "second part of list info" ?
30th Aug 2019, 10:22 AM
id001x
id001x - avatar
0
Welcome!!
1st Sep 2019, 3:55 PM
id001x
id001x - avatar