tags:

views:

50

answers:

4

I have the following code in C#:

IList<string> myList = null;
myList.Add(temp);

temp is a string that is decalred elsewhere and is not null (I checked it). I keep getting the following error at the line myList.Add(temp); "Object reference not initialised to an instance of an object"

What am I doing wrong here???

Updating the quesiton: I have already tried new
IList<string> myList = new List<string>();
as most of you suggested andget the following from Intellisense:

Cannot create an instance of the abstract class or interface Systems.Collections.Generic.Ilist.

Thanks for your previous answers. Now I am running into a peculiar problem. My datareader has an empty string in it. (1 field in the sql server table is blank. that is string.Empty). Is there a way to get rid of this inside the reader (I mean rdr here)??

+4  A: 

you need to initialize the list first:

IList<string> myList = new List<string>();
...

Please note that on the right of = you have to write List<string> not IList<string> since IList<string> is an interface, whereas List<string> is a class that implements that interface.

klausbyskov
updated question
VP
@VP updated my answer.
klausbyskov
Yup. I was not doing a new IList<string>(); Thank you.
VP
I just updated my question again for another problem
VP
A: 

The line:

IList<string> myList = null;

does not give you a list, but an empty reference to where a list could be.

IList<string> myList = new List<string>();

would properly instantiate myList, so you can use it (Add, Remove, etc.).

Mark Avenius
updated question.
VP
IList<string> is not a class, but rather an interface, so you cannot create and instance of it. You can, however, create an instance of List<string>, which implements IList<string>: `IList<string> myList = new *List*<string>();`
Mark Avenius
Thanks Mark. I got it.
VP
updated again :(
VP
A: 

Here, take a look at this tutorial to better understand variable initialization.

From http://www.csharphelp.com/2007/03/objects-classes-in-c/

Person Michael = new Person();

In the first line of code we specified integer variablecalled age. In the second line we specified first the type of Object we need tocreate followed by the object’s name followed by a reserved operator called new and we end by typing the class name again followed byparenthesis “()”.

Let’s understand it step-by-step. Specifying the class nameat the beginning tell the C# Compiler to allocate a memory location for thattype (C# compiler knows all the variables and properties and methods of theclass so it will allocate the right amount of memory). Then we followed theclass name by out object variable name that we want it. The rest of the code”=new Person();” callthe object’s constructor. We will talk about constructor later but for nowunderstand that the constructor is a way to initialize your object’s variablewhile you are creating it not after you create it. For example, The Michaelobject we created it in the last section can be written as following :

Person Michael = new Person(20, “Brown”);

here I specified the variable’s values in the parameter listso I initialized the variables while I’m creating the object. But for this codeto work we will need to specify the constructor in the Person class and I willnot do that here because constructor section will come in later articles. Ithink you got a good introduction about Classes and Objects not I will completein in my next article and I will talk about constructors and building blockscoping. I hope you got a new thing from my article.

Aaron
updated my question
VP
A: 

Updating the quesiton: I have already tried new Ilist() as most of you suggested andget the following from Intellisense: Cannot create an instance of the abstract class or interface Systems.Collections.Generic.Ilist

You cannot create instances of an Interface. IList<T> is an interface. Like the others have said here you initialize it with an instance of a concrete class that inherits the interface. Notice how they use new List<String>() not new IList<String>();

IList<String> items = new List<String>();
Brian
Yup thanks Brian. I learned my lesson..
VP
update again :(
VP
yeah sorry i saw the other guys comment after i submitted this =P. Trust me we've all done this before.
Brian