开发者

I'm having trouble assigning an initial size to a List<T> of classes

I am trying to initialize a list of objects (which are of the type Rep). Here is the code I am using to initialize the list:

        public static List<Rep> Reps = new List<Rep>(new Rep[6]);  

Right now when I try to assign a value to a string in one of the list's classes like this:

 Repository.Reps[repnum].Main = new TextRange(richTextBox1.Document.ContentStart,
 richTextBox1.Document.ContentEnd).Text;

I a null reference except开发者_StackOverflowion. What am I doing wrong? I couldn't find any Msdn documentation about setting the initial size of a list.


I think you might be getting your null reference exception because Repository.Reps[repnum] is null when you try to set the value of the Main property. What your doing is creating an Array of Reps with a size of 6, but all the references in that array you are giving to the List are null. Try newing up a Rep object and setting the Main property of it there like this:

Rep newRep = new Rep();
newRep.Main = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;

Repository.Reps[repnum] = newRep;

If your intention is let all values in the List be null at first it might just be simpler to use the List(int) constructor and create the List in this way:

public static List<Rep> Reps = new List<Rep>(6);

However, if your intention is to have your List contain not null objects when you create it you can create the List in this way:

public static List<Rep> Reps = new List<Rep>() 
{
    new Rep(),
    new Rep(),
    new Rep(),
    new Rep(),
    new Rep(),
    new Rep()
};


Here is an alternative way to fill the list with newly created instances of the Rep object using LINQ:

var reps = (from n in Enumerable.Range(0, 6)
            select new Rep()).ToList()

This generates a sequence of numbers from 0 to 5 (using Enumerable.Range) and then creates a new instance of Rep for each of the number (note that we don't really need the number n anywhere - it is used just to create some initial sequence of length 6. Then we can use ToList to convert the IEnumerable sequence to the List<Rep> type.

Alternatively using lambda functions explicitly (note that _ is a valid name of a variable in C#, but it gives us a nice syntax that suggests that we're actually ignoring the argument):

var reps = Enumerable.Range(0, 6).Select(_ => new Rep()).ToList()


The initial size is set correctly, it's what you put in the list that is the problem.

The expression new Rep[6] creates an array with six entries, but it will not create Rep instances for the entries, instead they are set to null.

So, you get a list containing six null references, not a list containing references to six Rep instances. When you try to use the Main property of an item in the list, you get a null reference exception.

If you want instances to be created, you have to specifically create them:

public static List<Rep> Reps = new List<Rep>() {
  new Rep(),
  new Rep(),
  new Rep(),
  new Rep(),
  new Rep(),
  new Rep()
};


Your list is initialized with 6 null references.

You have to assign a new Rep to each position.

Try:

    public static List<Rep> Reps = 
        new List<Rep>(new Rep[]{ 
            new Rep(),
            new Rep(),
            new Rep(),
            new Rep(),
            new Rep(),
            new Rep() });  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜