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. How to write a class, where one of its attributes is an Array?

How to write a class, where one of its attributes is an Array?

Scheduled Pinned Locked Moved C#
questiondata-structurestutorial
7 Posts 5 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.
  • N Offline
    N Offline
    nstk
    wrote on last edited by
    #1

    Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.

    class Testclass
    {
    ........

        private int\[\] \_number;
        public int number
        {
            get { return \_number\[\]; }
            set { \_number\[\] = value; }
        }
        
        ........
     }
    

    2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public public int[] number; Although I declare a new Object in the Form class (it should be a windows App) Testclass Myclass = new Testclass(); the programme crashes Myclass.number[i] = i + 2; saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me...

    P D R L 4 Replies Last reply
    0
    • N nstk

      Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.

      class Testclass
      {
      ........

          private int\[\] \_number;
          public int number
          {
              get { return \_number\[\]; }
              set { \_number\[\] = value; }
          }
          
          ........
       }
      

      2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public public int[] number; Although I declare a new Object in the Form class (it should be a windows App) Testclass Myclass = new Testclass(); the programme crashes Myclass.number[i] = i + 2; saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me...

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      There are a few things here. First of all, the return type from number would be int[] not int - the two are not comparable. Secondly, you don't return _number[], you return _number. Finally, why use an array at all? Arrays have the nasty habit of becoming complicated to manage - why not look into a generic List? So this becomes:

      class TestClass
      {
      public TestClass()
      {
      Number = new List<int>();
      }
      public List<int> Number { get; set; }
      }

      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      N 1 Reply Last reply
      0
      • N nstk

        Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.

        class Testclass
        {
        ........

            private int\[\] \_number;
            public int number
            {
                get { return \_number\[\]; }
                set { \_number\[\] = value; }
            }
            
            ........
         }
        

        2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public public int[] number; Although I declare a new Object in the Form class (it should be a windows App) Testclass Myclass = new Testclass(); the programme crashes Myclass.number[i] = i + 2; saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me...

        D Offline
        D Offline
        David1987
        wrote on last edited by
        #3

        class Testclass
        {
        private int[] _number;
        public int[] number
        {
        get { return _number; }
        set { _number = value; }
        }
        }

        number will be null unless you initialize it somehow.

        1 Reply Last reply
        0
        • N nstk

          Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.

          class Testclass
          {
          ........

              private int\[\] \_number;
              public int number
              {
                  get { return \_number\[\]; }
                  set { \_number\[\] = value; }
              }
              
              ........
           }
          

          2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public public int[] number; Although I declare a new Object in the Form class (it should be a windows App) Testclass Myclass = new Testclass(); the programme crashes Myclass.number[i] = i + 2; saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me...

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #4

          This is all you need:

          public class TestClass
          {
          public int[] Numbers { get; set; }
          }

          However, I would probably use a List in the following form:

          public class NumbersList : List<int>
          {
          // You don't have to put anything in the class if you don't want/need
          // to. It's just to clean up the allocation and type references because
          // typing "private NumbersList numbers; is cleaner than typing
          // "private List<int> numbers;" (at least, that's the way I see it).
          }

          ...which would allow you then to do this:

          public class TestClass
          {
          public NumbersList Numbers { get; set; }
          }

          ...as well as being much more flexible than a mere array.

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

          1 Reply Last reply
          0
          • N nstk

            Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.

            class Testclass
            {
            ........

                private int\[\] \_number;
                public int number
                {
                    get { return \_number\[\]; }
                    set { \_number\[\] = value; }
                }
                
                ........
             }
            

            2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public public int[] number; Although I declare a new Object in the Form class (it should be a windows App) Testclass Myclass = new Testclass(); the programme crashes Myclass.number[i] = i + 2; saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me...

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Ditto what others have said regarding arrays. Personally I'd only use an int[] if it is being initialised once, and then will not have elements added or removed. That could be useful for certain specific applications. For example...

            public class Simple
            {
                private int\[\] \_int = { 1, 2, 3, 4, 5 };
                public int\[\] MyInt { get { return \_int; } }
            }
            
            
            
                private static void TestSimple()
                {
                    Simple s = new Simple();
                    s.MyInt\[0\] += 1;
                }
            

            ...is fine. But as others have said if the size of the array is to be dynamic (items being added) I'd use say a List<int> probably.

            N 1 Reply Last reply
            0
            • L Lost User

              Ditto what others have said regarding arrays. Personally I'd only use an int[] if it is being initialised once, and then will not have elements added or removed. That could be useful for certain specific applications. For example...

              public class Simple
              {
                  private int\[\] \_int = { 1, 2, 3, 4, 5 };
                  public int\[\] MyInt { get { return \_int; } }
              }
              
              
              
                  private static void TestSimple()
                  {
                      Simple s = new Simple();
                      s.MyInt\[0\] += 1;
                  }
              

              ...is fine. But as others have said if the size of the array is to be dynamic (items being added) I'd use say a List<int> probably.

              N Offline
              N Offline
              nstk
              wrote on last edited by
              #6

              Thank you all for your help. This is a great forum indeed! I will definately use a List, as there will be numbers of unknown quantity and value in the beginning of the programme.

              1 Reply Last reply
              0
              • P Pete OHanlon

                There are a few things here. First of all, the return type from number would be int[] not int - the two are not comparable. Secondly, you don't return _number[], you return _number. Finally, why use an array at all? Arrays have the nasty habit of becoming complicated to manage - why not look into a generic List? So this becomes:

                class TestClass
                {
                public TestClass()
                {
                Number = new List<int>();
                }
                public List<int> Number { get; set; }
                }

                I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                N Offline
                N Offline
                nstk
                wrote on last edited by
                #7

                Is this a different list than an ArrayList? http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=VS.100).aspx

                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