1

I wanted to know if there is a syntax to add named objects to a list in its declaration so the end result is a list of objects that I can refer to them by name (without searching).

Here's the long way (which I know works), I have used a list of employees just to illustrate what I'm trying to do.

List<Employee> Employees = new List<Employee>();

Employee Tom = new Employee(id = 001, Department = "Sales");
Employee Joan = new Employee(id = 002, Department= "HR" );
Employee Fred = new Employee(id = 003, Department= "Accounting");

Employees.Add(Tom);
Employees.Add(Joan);
Employees.Add(Fred);

So the above works, I have named objects in a list which I can then use.

You can also declare the Employees when you declare the list:

List<Employee> employees= new List<Employee>()
{
    new Employee() { empID = 001, Name = "Tom", Department= "Sales"},
    new Employee() { empID = 004, Name = "Joan", Department= "HR"},
    new Employee() { empID = 003, Name = "Fred", Department= "Accounting" },
};

However, I have to create the Name as a string in the class, instead of the Employee object.

What I want to know is, "Is there a way to do this?"

List<Employee> employees= new List<Employee>()
{
    new Employee() Tom = { empID = 001,  Department= "Sales"},
    new Employee() Joan = { empID = 024, Department= "HR"},
    new Employee() Fred = { empID = 023, Department= "Accounting" },
};

^^Obviously, the above doesn't work, but hopefully it shows what I am trying to achieve.

Edit: thanks for the answers @Ann L and D Stanley, I think I need to elaborate further on where I'm trying to put, your example works here:

<script runat="server">

Employee Tom = new Employee(001, "Sales");
Employee Joan = new Employee(002, "HR");
Employee Fred = new Employee(003, "Accounting");

public List<Employee> Employees;

protected override void OnLoad(EventArgs e)
{     
    Employees = new List<Employee>() { Tom, Joan, Fred };

    base.OnLoad(e);
}
</script>

So that my list is publicly accessible, however, it defeats my goal of having the declaration and assignment in the same place, as the below does NOT work

<script runat="server">

Employee Tom = new Employee(001, "Sales");
Employee Joan = new Employee(002, "HR");
Employee Fred = new Employee(003, "Accounting");

public List<Employee> Employees = new List<Employee>() { Tom, Joan, Fred };

protected override void OnLoad(EventArgs e)
{     


    base.OnLoad(e);
}
</script>

On the above, I get the error "A field initializer cannot reference the non-static field, method or property"

15
  • 4
    No, because the names don't have any meaning since they would disappear outside the scope of the construction block. If you need to access them by name later, you should use a Dictionary<string, Employee>. Commented Jul 15, 2015 at 12:40
  • Annonymous Types are not strongly typed. Commented Jul 15, 2015 at 12:41
  • You could create the list after assigning your variables: employees = new List<Employee> { Tom, Joan, Fred } Commented Jul 15, 2015 at 12:42
  • @AmitKumarGhosh nothing in the example is an anonymous type. Commented Jul 15, 2015 at 12:42
  • 26 second response time has to be some kind of record, thanks Commented Jul 15, 2015 at 12:42

3 Answers 3

4

Are you looking for dictionary?

Dictionary<String, Employee> employees = new Dictionary<String, Employee>() {
  {"Tom", new Employee() { empID = 001,  Department= "Sales"}},
  {"Joan", new Employee() { empID = 024, Department= "HR"}},
  {"Fred", new Employee() { empID = 023, Department= "Accounting"}},
};

...

Employee sample = employees["Joan"];
Sign up to request clarification or add additional context in comments.

4 Comments

This is decent.But I wonder Why wouldn't I make a Employee class instead.
@AmitKumarGhosh I'm not sure what you are getting at, Employee is a class.
I like this so much, I deleted my own answer :)
my eyes are gone blur
1

No - you have to choose variables or initializer syntax - there's no way to declare a variable inside the initialization.

A slight compromise would be:

Employee Tom = new Employee(id = 001, Department = "Sales");
Employee Joan = new Employee(id = 002, Department= "HR" );
Employee Fred = new Employee(id = 003, Department= "Accounting");

List<Employee> Employees = new List<Employee>() {Tom, Joan, Fred};

2 Comments

Thanks, please see my edited question above with your solution
@MrGiggles So those are class members? Class members have more restrictions on using initializers. You could make the Employee fields static but that may have unintended consequences since they would be shared across all requests. If the values don't change then it shouldn't be a problem.
1

Would this work for you?

Employee Tom = new Employee(id = 001, Department = "Sales");
Employee Joan = new Employee(id = 002, Department= "HR" );
Employee Fred = new Employee(id = 003, Department= "Accounting");

List<Employee> Employees = new List<Employee>() {
    Tom, Joan, Fred 
}

Not that different from what you wrote, but a variation you didn't list.

EDIT: The error message you get seems pretty adamant: a field initializer (like the = new Employee() part of the declarations) cannot refer to non-static fields.

So if you want to initialize a field to an expression that involves other non-static fields (like new List<Employee> { Bob, Joan, Fred }, you can't do it.

But you could try this, or some variant. As you wished, your declaration and your initialization would be in the same place.

List<Employee> Employees {
   get {
      return new List<Employees>() { Tom, Joan, Fred }
   }
}   

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.