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 exception. 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:
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:
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: