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. Array object Empty

Array object Empty

Scheduled Pinned Locked Moved C#
data-structuresquestion
8 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.
  • M Offline
    M Offline
    MrJJKoolJ
    wrote on last edited by
    #1

    Hi, I need to test if an Array object is empty. What's the best way to do this? Also how do you determine if an object exists? Thanks, JJ

    C O S 3 Replies Last reply
    0
    • M MrJJKoolJ

      Hi, I need to test if an Array object is empty. What's the best way to do this? Also how do you determine if an object exists? Thanks, JJ

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      MrJJKoolJ wrote: I need to test if an Array object is empty. What's the best way to do this? Do you mean contains no elements, or that all the elements are null? The Length property will tell you how many elements are in an array. If you mean the second option then you would have to iterate over the array testing for null on each element. MrJJKoolJ wrote: Also how do you determine if an object exists? I don't understand this at all. If an object exists you will have a reference to it somewhere. Although, sometime objects will exist that you don't have a reference to anymore and by that time it is too late because the garbage collector will be clearing it out soon anyway.


      "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

      M 1 Reply Last reply
      0
      • M MrJJKoolJ

        Hi, I need to test if an Array object is empty. What's the best way to do this? Also how do you determine if an object exists? Thanks, JJ

        O Offline
        O Offline
        Otis_69
        wrote on last edited by
        #3

        To determine if the object exists you do this: if(yourobject != null) { //object doesnt exist, do something about it } else { //object does exist } I hope you have the right word (exists) because an object that doesnt exist is just a name. If it does exist, then it has a refrence to a location in memory that holds the object. To test if the array is empty, do as the guy before me said, use yourarray.length() because by empty, i assume you mean the name exists, which holds a refrence to the object (so the object exists) but there is no data in the elements... //Otis

        M 1 Reply Last reply
        0
        • M MrJJKoolJ

          Hi, I need to test if an Array object is empty. What's the best way to do this? Also how do you determine if an object exists? Thanks, JJ

          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          If you want to determine if the elements of your Array reference an object of a class, you have to iterate over your Array and test them against null, like Colin said. Alternatively, you could use an ArrayList instead of the Array. The ArrayList class provides the Count property which tells how many objects really exist in your ArrayList. www.troschuetz.de

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            MrJJKoolJ wrote: I need to test if an Array object is empty. What's the best way to do this? Do you mean contains no elements, or that all the elements are null? The Length property will tell you how many elements are in an array. If you mean the second option then you would have to iterate over the array testing for null on each element. MrJJKoolJ wrote: Also how do you determine if an object exists? I don't understand this at all. If an object exists you will have a reference to it somewhere. Although, sometime objects will exist that you don't have a reference to anymore and by that time it is too late because the garbage collector will be clearing it out soon anyway.


            "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

            M Offline
            M Offline
            MrJJKoolJ
            wrote on last edited by
            #5

            That the array contains no elements. Also How do you test if all the elements are null? On the object question, I need to know how to test if an object exists for a reference. Thanks, JJ

            C 1 Reply Last reply
            0
            • O Otis_69

              To determine if the object exists you do this: if(yourobject != null) { //object doesnt exist, do something about it } else { //object does exist } I hope you have the right word (exists) because an object that doesnt exist is just a name. If it does exist, then it has a refrence to a location in memory that holds the object. To test if the array is empty, do as the guy before me said, use yourarray.length() because by empty, i assume you mean the name exists, which holds a refrence to the object (so the object exists) but there is no data in the elements... //Otis

              M Offline
              M Offline
              MrJJKoolJ
              wrote on last edited by
              #6

              Exactly Thanks Otis JJ

              O 1 Reply Last reply
              0
              • M MrJJKoolJ

                That the array contains no elements. Also How do you test if all the elements are null? On the object question, I need to know how to test if an object exists for a reference. Thanks, JJ

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                MrJJKoolJ wrote: Also How do you test if all the elements are null?

                bool allNull = true;
                foreach(object element in myArray)
                {
                if (element != null)
                {
                allNull=false;
                break;
                }
                }

                At the end of this code snippit, allNull will be true if all the elements of the array are null. It will also be true if the array is of zero length (so you might need to test for that condition also) MrJJKoolJ wrote: On the object question, I need to know how to test if an object exists for a reference Okay, a bit of confused terminology here. You want to tell if a "variable" references an object or not. For that, it looks like Otis_69 responded with the correct answer for that. Does this help?


                "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

                1 Reply Last reply
                0
                • M MrJJKoolJ

                  Exactly Thanks Otis JJ

                  O Offline
                  O Offline
                  Otis_69
                  wrote on last edited by
                  #8

                  no problem... All these other people may be guru's, and thats why they dont understand that you want a simple answer... Where as i just learned that stuff a few weeks ago in college, and im new to C#, i can help you on a more "noob" level (no offense, if any)... Thats the problem with smart people and trying to help others, they think that we know a shitload of stuff, when they gotta think like the person they are helping... :P I've seen it happen too many times, id ask something, and a whole bunch of guru's will come along and say "do this, do that" and it gets so complicated, they seem to think im whining, when i just cant understand, and then they tell me i cant program C# anymore because i have to know everything... then a someone with just a bit more skill comes along, and tells me what to do... and im satisfied. I say that noone should ever ask a guru what might seem to be a simple question to them. Well, thats just my little grudge, ill prolly get flamed for it, and called a whiner, when im just stating my opinion, with some facts :P I just ignore them :P //Otis

                  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