Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an array of OleDbparameter

Status
Not open for further replies.

Susmita

Programmer
Jan 9, 2006
4
US
Dear Experts,
I was trying to create an array of OleDbParameter using the following code snippet, but it gave me a NullReference Exception - "Object Reference not set to an instance of an object."

OleDbParameter [] params = new OleDbParameter[2];
SPparams[0].ParameterName = "BeginningDate";
SPparams[0].Value = Convert.ToDateTime("1/1/95");


But if I changed the above array declaration/creation statement to this, it works fine:

OleDbParameter [] params = {new OleDbParameter(), new OleDbParameter()};

My question is: Is the first-form not the correct way of declaring and creating an array? Pls. note that similar code has been used in msdn for illustrating the usage of CopyTo() method of OleDbParameterCollection class.

Thanks in advance.
Susmita
 
OleDbParameter [] params = new OleDbParameter[2];

SPparams[0] = new OleDbParameter();

SPparams[0].ParameterName = "BeginningDate";
SPparams[0].Value = Convert.ToDateTime("1/1/95");
 
Thanks for the reply.
But I would like to know what does the statement
OleDbParameter [] params = new OleDbParameter[2];
exactly do? I thought that it allocates and initializes the memory for the array.

Susmita
 
It certainly allocates the memory but it does not initialize it. You have to create the object and assign it to that "reserved" chunk of memory.

When you do { new object1, new object2, new object3 } you are actually creating new objects directly into the array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top