Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reseting Object Arrays With New Length

Reseting Object Arrays With New Length

Scheduled Pinned Locked Moved C#
helpquestioncssdata-structurestutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    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?

    L S J 3 Replies Last reply
    0
    • B budidharma

      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?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      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!

      B 1 Reply Last reply
      0
      • L leppie

        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!

        B Offline
        B Offline
        budidharma
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • B budidharma

          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?

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • B budidharma

            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?

            J Offline
            J Offline
            Jon Rista
            wrote on last edited by
            #5

            Before I answer, how many times does the last foreach loop? Does it loop twice, then give the nullexception? Or does it immediately give the nullexception on thefirst iteration?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups