Reseting Object Arrays With New Length
-
Here's an example of what I'm dealing with: int[] myints = new int { 1, 2, 3 }; foreach(int i in myints) { Console.Write(i.ToString()); // WORKS PERFECT. } Console.Write(myints.Length); // Displays 3. int[] newints = new int { 1, 2, 3, 4 }; myints = newints; foreach(int i in myints) { Console.Write(i.ToString()); // WORKS PERFECT. } Console.Write(myints.Length); // Displays 4 int[] reallynewints = new int { 1, 2 }; myints = reallynewints; foreach(int i in myints) { Console.Write(i.ToString()); // THROWS NULLEXCEPTIONERROR } Console.Write(myints.Length); // Displays 2 So, here's the problem I'm having. When you set an array equal to an array with less elements, the Length property is updated, but there is still a null reference inside the array to an unused array element. You can fix the problem by overloading the Int.ToString() method with a try-catch block to avoid the error, but that's just a patch. How can I reset the array completely, so there is not a null reference to unused elements?
-
Here's an example of what I'm dealing with: int[] myints = new int { 1, 2, 3 }; foreach(int i in myints) { Console.Write(i.ToString()); // WORKS PERFECT. } Console.Write(myints.Length); // Displays 3. int[] newints = new int { 1, 2, 3, 4 }; myints = newints; foreach(int i in myints) { Console.Write(i.ToString()); // WORKS PERFECT. } Console.Write(myints.Length); // Displays 4 int[] reallynewints = new int { 1, 2 }; myints = reallynewints; foreach(int i in myints) { Console.Write(i.ToString()); // THROWS NULLEXCEPTIONERROR } Console.Write(myints.Length); // Displays 2 So, here's the problem I'm having. When you set an array equal to an array with less elements, the Length property is updated, but there is still a null reference inside the array to an unused array element. You can fix the problem by overloading the Int.ToString() method with a try-catch block to avoid the error, but that's just a patch. How can I reset the array completely, so there is not a null reference to unused elements?
budidharma wrote:
foreach(int i in myints) { Console.Write(i.ToString()); // THROWS NULLEXCEPTIONERROR } Console.Write(myints.Length); // Displays 2
How it display 2 if you got a NullReference exception? xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!
-
budidharma wrote:
foreach(int i in myints) { Console.Write(i.ToString()); // THROWS NULLEXCEPTIONERROR } Console.Write(myints.Length); // Displays 2
How it display 2 if you got a NullReference exception? xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!
The way I just wrote that, it wouldn't cause the error would occur first. If you put the myints.Length line before the foreach statement, it will display the correct length.
-
Here's an example of what I'm dealing with: int[] myints = new int { 1, 2, 3 }; foreach(int i in myints) { Console.Write(i.ToString()); // WORKS PERFECT. } Console.Write(myints.Length); // Displays 3. int[] newints = new int { 1, 2, 3, 4 }; myints = newints; foreach(int i in myints) { Console.Write(i.ToString()); // WORKS PERFECT. } Console.Write(myints.Length); // Displays 4 int[] reallynewints = new int { 1, 2 }; myints = reallynewints; foreach(int i in myints) { Console.Write(i.ToString()); // THROWS NULLEXCEPTIONERROR } Console.Write(myints.Length); // Displays 2 So, here's the problem I'm having. When you set an array equal to an array with less elements, the Length property is updated, but there is still a null reference inside the array to an unused array element. You can fix the problem by overloading the Int.ToString() method with a try-catch block to avoid the error, but that's just a patch. How can I reset the array completely, so there is not a null reference to unused elements?
budidharma wrote:
When you set an array equal to an array with less elements, the Length property is updated, but there is still a null reference inside the array to an unused array element.
That can't be true. Arrays being reference types, everytime you assign something to myints, the earlier reference is overwritten with the new one. So
myints = reallynewints
maeks myints point to the reallynewints array now. The source of your problem is somewhere else. Are you doing something to the myints array after the assignment but before the foreach statement? Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
Here's an example of what I'm dealing with: int[] myints = new int { 1, 2, 3 }; foreach(int i in myints) { Console.Write(i.ToString()); // WORKS PERFECT. } Console.Write(myints.Length); // Displays 3. int[] newints = new int { 1, 2, 3, 4 }; myints = newints; foreach(int i in myints) { Console.Write(i.ToString()); // WORKS PERFECT. } Console.Write(myints.Length); // Displays 4 int[] reallynewints = new int { 1, 2 }; myints = reallynewints; foreach(int i in myints) { Console.Write(i.ToString()); // THROWS NULLEXCEPTIONERROR } Console.Write(myints.Length); // Displays 2 So, here's the problem I'm having. When you set an array equal to an array with less elements, the Length property is updated, but there is still a null reference inside the array to an unused array element. You can fix the problem by overloading the Int.ToString() method with a try-catch block to avoid the error, but that's just a patch. How can I reset the array completely, so there is not a null reference to unused elements?