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. .Count, not in namespace system.

.Count, not in namespace system.

Scheduled Pinned Locked Moved C#
data-structuresquestion
12 Posts 6 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.
  • J jblouir

    In a previous post I was informed that I could count the number of elements in an array using. // elements is a previously declared array elements.Count however .Count isnt available I looked it up in the object viewer and it looks like its a part of the system namespace which I have included in my code. Anyone know why it might not be showing up? and also if elements.Count and elements.Length return the same value?

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

    It is an array then use Length. If it any other type of collection then use Count.


    Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

    D 1 Reply Last reply
    0
    • C Colin Angus Mackay

      It is an array then use Length. If it any other type of collection then use Count.


      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      D Offline
      D Offline
      DavidNohejl
      wrote on last edited by
      #4

      Colin Angus Mackay wrote:

      It is an array then use Length. If it any other type of collection then use Count

      Kinda sucks there is this "inconsistency". Yes, sure, logicaly array has lenght not count and collection doesn't have lenght but count, but... What about NumberOfElements or something for both.


      "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

      1 Reply Last reply
      0
      • J jblouir

        In a previous post I was informed that I could count the number of elements in an array using. // elements is a previously declared array elements.Count however .Count isnt available I looked it up in the object viewer and it looks like its a part of the system namespace which I have included in my code. Anyone know why it might not be showing up? and also if elements.Count and elements.Length return the same value?

        J Offline
        J Offline
        jblouir
        wrote on last edited by
        #5

        Well I was unstructed to use this... string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; elements is an array, when .Count didnt work I assumed it was because I missing a using namespace .Length appears to work but im still stuck with trying to add an object to a global object array. intIndex, strRoom,strDesc,strLook, i are all variables, descriptor being the object name RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.arrRoomData.SetValue(descriptor,i) i++;

        D C L 3 Replies Last reply
        0
        • J jblouir

          Well I was unstructed to use this... string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; elements is an array, when .Count didnt work I assumed it was because I missing a using namespace .Length appears to work but im still stuck with trying to add an object to a global object array. intIndex, strRoom,strDesc,strLook, i are all variables, descriptor being the object name RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.arrRoomData.SetValue(descriptor,i) i++;

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #6

          jblouir wrote:

          but im still stuck with trying to add an object to a global object array.

          What is the problem? If fixed lenght of array is problem, use collection.


          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          1 Reply Last reply
          0
          • J jblouir

            Well I was unstructed to use this... string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; elements is an array, when .Count didnt work I assumed it was because I missing a using namespace .Length appears to work but im still stuck with trying to add an object to a global object array. intIndex, strRoom,strDesc,strLook, i are all variables, descriptor being the object name RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.arrRoomData.SetValue(descriptor,i) i++;

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

            jblouir wrote:

            im still stuck with trying to add an object to a global object array

            You can't add objects to an array - They have a fixed length. You may wish to try an ArrayList. Also, global variables are rarely justified in an object oriented language. You may wish to have a rethink about your design.


            Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

            J 1 Reply Last reply
            0
            • J jblouir

              Well I was unstructed to use this... string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; elements is an array, when .Count didnt work I assumed it was because I missing a using namespace .Length appears to work but im still stuck with trying to add an object to a global object array. intIndex, strRoom,strDesc,strLook, i are all variables, descriptor being the object name RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.arrRoomData.SetValue(descriptor,i) i++;

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #8

              Hi, some remarks on your code:

              jblouir wrote:

              string [] elements = line.split(new char [] { ',' } );

              can be simplified to string [] elements = line.Split(','); because of the params keyword in one of the Split overloads

              jblouir wrote:

              if (elements.Count > 0)

              I see no way elements.Count could be zero; if no comma's are present, everything will be returned as the first and only string in elements[]. :)

              Luc Pattyn [My Articles] [Forum Guidelines]

              C 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, some remarks on your code:

                jblouir wrote:

                string [] elements = line.split(new char [] { ',' } );

                can be simplified to string [] elements = line.Split(','); because of the params keyword in one of the Split overloads

                jblouir wrote:

                if (elements.Count > 0)

                I see no way elements.Count could be zero; if no comma's are present, everything will be returned as the first and only string in elements[]. :)

                Luc Pattyn [My Articles] [Forum Guidelines]

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #9

                Luc Pattyn wrote:

                string [] elements = line.Split(',');

                He's trying to use code that I wrote off the cuff in an earlier answer. I have found that the above doesn't work for me, I must be doing something wrong.

                Luc Pattyn wrote:

                I see no way elements.Count could be zero;

                Good point, I was trying to illustrate the need to code defensively, and not assume an array length, but in this case, you're right.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                1 Reply Last reply
                0
                • C Colin Angus Mackay

                  jblouir wrote:

                  im still stuck with trying to add an object to a global object array

                  You can't add objects to an array - They have a fixed length. You may wish to try an ArrayList. Also, global variables are rarely justified in an object oriented language. You may wish to have a rethink about your design.


                  Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                  J Offline
                  J Offline
                  jblouir
                  wrote on last edited by
                  #10

                  Well I have a game loop in void Main, and I have a few methods, one being initialise which sits in the loop that repeats until input = exit. the loop needs the input variable and initialise needs the input variable they need to be the same, and the best way I found of doing that is using a global variable. I have been literally programming in C# for less than a week, I am doing things that work and learning as I go, if there is a better way to do it then I implement the new method. I had thought that I could make a method that would return input but couldnt work it out at the time. // Edit And yes, the internet is my only resource at the moment, when I get the money to spend ill buy a book on C#.

                  1 Reply Last reply
                  0
                  • B Brady Kelly

                    How can a property be part of the System namespace? Elements.Count won't ever return anything, so do yourself a favour and use the property that does exist, elements.Length

                    J Offline
                    J Offline
                    jblouir
                    wrote on last edited by
                    #11

                    Forgive me if I am wrong but dont all the different classes such as array and int etc come from namespaces? Then .Count is derived from array which is part of system, ok so its a property, but if you dont have proper namespace it wont be there.

                    B 1 Reply Last reply
                    0
                    • J jblouir

                      Forgive me if I am wrong but dont all the different classes such as array and int etc come from namespaces? Then .Count is derived from array which is part of system, ok so its a property, but if you dont have proper namespace it wont be there.

                      B Offline
                      B Offline
                      Brady Kelly
                      wrote on last edited by
                      #12

                      Count is a property, it isn't found in a namespace but on a class, and the class will be found in a namespace.

                      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