C
C#2y ago
jakub

❔ As loop value increments, increment variable name

I have a loop to insert values into 5 objects of employees named employee1,employee2... Is there a way I can make the loop update the number in the variable name after each iteration? I am very new to C# so I am looking for any sort of help.
3 Replies
vdvman1
vdvman12y ago
No, but you could store the employees in an array instead of separate variables
Employee[] employees = new Employee[5];
for (int i = 0; i < employees.Length; ++i)
{
employees[i] = new Employee(...);
...
}
Employee[] employees = new Employee[5];
for (int i = 0; i < employees.Length; ++i)
{
employees[i] = new Employee(...);
...
}
jakub
jakubOP2y ago
Thank you very much 🙂 I was thinking of using an array however I trying to find out if C# had the capability of doing what I requested. Thank you again.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server