DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Null reference in array (http://www.daniweb.com/forums/thread166872.html)

ddanbe Jan 7th, 2009 5:15 pm
Null reference in array
 
When I want an array of integers with 4 rows and 2 columns I do
int[,] array = new int[4, 2];
and then use array[0, 0]=123; to set the first element.
So for the matrix input usercontrol I'm working on I thought I need something like :
TextBox[,] elements = new TextBox[5,5];(This compiles)
but when I try to access an element of this array ( e.g. elements[0,0].Name="foo";) I get a Null ref exeption.
When I do
elements[r, c] = new TextBox();
elements[r, c].Name = "foo";
All is well.
I'm confused here! Why two times new TextBox?

Ramy Mahrous Jan 7th, 2009 5:58 pm
Re: Null reference in array
 
int value type.. TextBox reference type you should new it...

ddanbe Jan 8th, 2009 6:53 pm
Re: Null reference in array
 
Phew!
More than 10000 views in one day. Seems to me a lot of people have the same problem I had.

Ramy Mahrous Jan 8th, 2009 10:09 pm
Re: Null reference in array
 
I am proud I could solve it to my expert :)


All times are GMT -4. The time now is 7:05 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC