tags:

views:

164

answers:

8

I have a variable FirstThreads of type List<Thread>.

I am trying to do the following, but FirstThreads is always null.

FirstThreads.AddRange(Threads.Skip<Thread>(PageIndex * PageSize)
            .Take<Thread>(PageSize));

I can't do this:

FirstThreads = FirstThreads.AddRange(Threads.Skip<Thread>(PageIndex * PageSize)
                                            .Take<Thread>(PageSize));

Do you have any idea how to solve this?

+8  A: 

Before you can interact with the FirstThreads variable, you need to make the variable refer to a List<Thread> instance, like this:

firstThreads = new List<Thread>();

You can also write

firstThreads = Threads.Skip<Thread>(PageIndex * PageSize).Take<Thread>(PageSize)
SLaks
+2  A: 

you need to instantiate the list first, I assume it is a List<Thread>?

so

FirstThreads = new List<Thread>();
Pharabus
+4  A: 

You need to initialize your List<FirstThreads> first.

The default value of any object is null.

For example:

List<FirstThreads> firstThreads = new List<FirstThreads>();
firstThreads.AddRange(collection);
Kevin
+2  A: 

You need to instantiate the List object:

List<Thread> FirstThreads = new List<Thread>();
GenericTypeTea
A: 

You can use

FirstThread = 
    new List<Thread>(Threads.Skip<Thread>(PageIndex * PageSize)
                            .Take<Thread>(PageSize));

Or if you're not sure whether it has items and is already instantiated

(FirstThread ?? FirstThread = new List<Thread>())
    .AddRange(Threads.Skip<Thread>(PageIndex * PageSize)
    .Take<Thread>(PageSize));

Note:

You can probably take out the <Thread> for the Skip and Take methods, as it will be inferred.

Yuriy Faktorovich
A: 

THe thing is, if you write FirstThreads.AddRange you're implying FirstThreads is an object with the AddRange method; but FirstThreads is null, that means that it's empty (so it has no methods).
You would have to make FirstThreads an empty list:

FirstThreads = new List<Thread>();
Yandros
A: 

FirstThreads either was set to null or is never initialized. Either way my guess is you need to create an object to assign to it.

FirstThreads = new <-- visual studio will auto complete object creation code

Gabriel
+1  A: 

When in doubt the 'new' keyword is always worth a shot