1

So I'm creating a new tool in C# and I've created a class called 'Customer' and each 'Customer' has sub-group of employees which is an array of names. How do I set this property in my class for 'Customer'? What I have below for 'Employees' is not correct. I just left it there as a placeholder. Thank you

public class Customer
{
    public String Number { get; set; }
    public String Name { get; set; }
    public String Street { get; set; }
    public String City { get; set; }
    public String State { get; set; }
    public String Zipcode { get; set; }
    public string[] Employees = { get; set; }
}
7
  • Why don't you use a List instead? List<string> Commented Sep 11, 2014 at 4:29
  • How would i write that properly in my code? Commented Sep 11, 2014 at 4:30
  • What's wrong with public string[] Employees { get; set; }? Commented Sep 11, 2014 at 4:31
  • You just need to remove the '=' sign Commented Sep 11, 2014 at 4:31
  • 1
    I'm using List instead. I'm always for learning and improving. Thanks Hassan! Commented Sep 11, 2014 at 5:36

2 Answers 2

3

You could use a List instead of an array as it is easier to manipulate:

public class Customer
{
    public String Number { get; set; }
    public String Name { get; set; }
    public String Street { get; set; }
    public String City { get; set; }
    public String State { get; set; }
    public String Zipcode { get; set; }
    public List<string> Employees { get; set; }
}

And then when you instantiate it, you could add new employers as such:

Customer customer = new Cusomter();
customer.Number = "num1";
customer.Name = "ABC";
//...

List<string> lstEmp = new List<string>();
lstEmp.Add("NewEmployee1");
lstEmp.Add("NewEmployee2");

customer.Employees = lstEmp;

And read it like this:

foreach (string name in customer.Employees)
{
    Console.WriteLine(name);
}
Sign up to request clarification or add additional context in comments.

2 Comments

List<string> is better choice :)
I'm going to use this List method as it's the better way to go based on everything you guys are saying . Thanks again guys
1

Simply use this declaration:

public string[] Employees { get; set; }

Comments

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.