List<> - What am I doing wrong here?

OK, I'm writing an app for managing citation records. I've choosen to use List<> arrays to contain elements with variable numbers of instances in each record such as the Authors. Right now, I am running into 2 problems. My code is down at the end of this post.

1: In my default constructor, I want to initialize all of the List<>s with default values. Unfortunately, I can't figure out how to create an array without declaring a temporary variable and then adding it to the List<> for every element. While the current method works, having all these temp variables in my constructor strikes me as poor programming technique. Is there any way to get a List<> constructor to simply initialize the appropriate type variable when it is created

For example, if I declare a new List<Person>(5), how do I get the List<> to create 5 entries that actually contain Person structs without having to declare 5 different temp Person structs and assign them to the List<> elements individually

2: The last line of the code listed below causes the following compile error:

Error 1 Cannot modify the return value of 'System.Collections.Generic.List<ART_Citation_Manager.Person>.this[int]' because it is not a variable C:\Documents and Settings\**********\My Documents\Visual Studio 2005\Projects\ART Citation Manager\ART Citation Manager\Citation.cs 56 13 ART Citation Manager

How can I change the values stored in the Person struct after the list has been created If I directly reference this.Authors[0].FirstName, as I am trying to do in the code below, I get that compile error. If I do this instead:

Person tempPerson = this.Authors[0];

tempPerson.FirstName = "foo";

the variable does not get updated. I can't possibly imagine that List<> was designed without a way to update class and struct elements stored in it. Can anyone tell me how this is done

Thanks!

public struct Person

{

public String LastName;

public String FirstName; //first name or initials

}

[public class Citation

{

<Snip>

public List<Person> Authors;

<Snip>

public Citation() //default constructor

{

<Snip>

Person Authors = new Person();

Authors.FirstName = "Authors.FirstName";

Authors.LastName = "Authors.LastName";

this.Authors = new List<Person>(1);

this.Authors.Add(Authors);

//Authors = this.Authors[0];

this.Authors[0].FirstName = "foo";

<Snip>

}



Answer this question

List<> - What am I doing wrong here?

  • MalcolmB

    thanx!



  • Zaph0d

    Thanks David, that did the trick. I subsequently ran into the problem of how to handle some List<string>s that I had in the class but thrashed around until I figured it out.

    this.UID = new List<string>(1);

    this.UID.Add(new String(' ', 0));

    this.UID[0] = "UID";

    In case anyone following this thread was wondering. The string constructor won't take a null char as input so a zero length set of spaces works instead.

    Dan


  • Gordon Pollokoff

    Dan,

    Don't make Person a struct, instead change it to a class. To initialize a List<> with defaults simply do the following:



    List<Person> list = new List<Person>();
    for (int i = 0; i < 5; i++)
    {
    list.Add(new Person());
    }



  • List<> - What am I doing wrong here?