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. Arrays in a class definition

Arrays in a class definition

Scheduled Pinned Locked Moved C#
helpdatabasedata-structurestutorial
6 Posts 3 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.
  • I Offline
    I Offline
    IceWater42
    wrote on last edited by
    #1

    Hi ... I'm having trouble setting the values of the array in my class. I tried this ... and i can set/get "x.name" but pgm crashes with an error message that says i do not have an instance of the array item when i try to set "x.number[1]". namespace WindowsApplication1 { public partial class Form1 : Form { class myClass { // fields private string _name; private double[] _number; // properties public string name { get { return _name; } set { _name = value; } } public double[] number; public double this[int index] { get { return _number[index]; } set { _number[index] = value; } } // Default constructor: public myClass() { name = ""; double[] number = new double[5]; number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; number[4] = 0; } // Clear Numbers public void ClearNumbers() { double[] number = new double[5]; number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; number[4] = 0; } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { myClass x = new myClass(); x.name = "Charlie"; x.number[1] = 123.45; } } } My program works on x.name = "Charlie"; My program fails on x.number[1] = 123.45; Please advise on how to fix my class definition to accomodate arrays. Thank you. -- modified at 10:17 Saturday 25th March, 2006

    G 1 Reply Last reply
    0
    • I IceWater42

      Hi ... I'm having trouble setting the values of the array in my class. I tried this ... and i can set/get "x.name" but pgm crashes with an error message that says i do not have an instance of the array item when i try to set "x.number[1]". namespace WindowsApplication1 { public partial class Form1 : Form { class myClass { // fields private string _name; private double[] _number; // properties public string name { get { return _name; } set { _name = value; } } public double[] number; public double this[int index] { get { return _number[index]; } set { _number[index] = value; } } // Default constructor: public myClass() { name = ""; double[] number = new double[5]; number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; number[4] = 0; } // Clear Numbers public void ClearNumbers() { double[] number = new double[5]; number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; number[4] = 0; } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { myClass x = new myClass(); x.name = "Charlie"; x.number[1] = 123.45; } } } My program works on x.name = "Charlie"; My program fails on x.number[1] = 123.45; Please advise on how to fix my class definition to accomodate arrays. Thank you. -- modified at 10:17 Saturday 25th March, 2006

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      The code in the constructor is not even possible to compile. Why don't you show the code that you really are using? I assume that the code in the constructor looks like the code in the ClearNumbers method, as that at least compiles... You have two array references in the class, _number and number. You never assign anything to any of them, so of course you get an error message when you try to use any of them. If you look at the task list after compiling, you will see that the compiler has given you very informative warnings about this. --- b { font-weight: normal; }

      I 1 Reply Last reply
      0
      • G Guffa

        The code in the constructor is not even possible to compile. Why don't you show the code that you really are using? I assume that the code in the constructor looks like the code in the ClearNumbers method, as that at least compiles... You have two array references in the class, _number and number. You never assign anything to any of them, so of course you get an error message when you try to use any of them. If you look at the task list after compiling, you will see that the compiler has given you very informative warnings about this. --- b { font-weight: normal; }

        I Offline
        I Offline
        IceWater42
        wrote on last edited by
        #3

        Sorry ... i have corrected the original post. i should have just copy/pasted the whole original .. i did not actually save that much space when i copied just part of the problem ... and i introduced an error. Thank you for pointing that out. As you can tell ... i am a newbie at class definitions. I have no idea why the underscore character precedes the field names and then (if needed at all) why it is not used in EVERY reference to the field. But not having a knowledgeable mentor ... i am subject to the problems associated with "mimicking" what i have seen in other class definitions. I think u get the idea of what i am trying to accomplsh. Please suggest code change to the class so the button code works. Thanks.

        K 1 Reply Last reply
        0
        • I IceWater42

          Sorry ... i have corrected the original post. i should have just copy/pasted the whole original .. i did not actually save that much space when i copied just part of the problem ... and i introduced an error. Thank you for pointing that out. As you can tell ... i am a newbie at class definitions. I have no idea why the underscore character precedes the field names and then (if needed at all) why it is not used in EVERY reference to the field. But not having a knowledgeable mentor ... i am subject to the problems associated with "mimicking" what i have seen in other class definitions. I think u get the idea of what i am trying to accomplsh. Please suggest code change to the class so the button code works. Thanks.

          K Offline
          K Offline
          kasik
          wrote on last edited by
          #4

          You have two arrays, like Guffa said, number and _number and you dont assign anything to either of them. The fact that they have the same name, give or take an underscore, means nothing, they are completely different variables that are in no way related to each other. Your code crashes on x.number[1] because you havent initialised number to anything, and so it is currently null. So trying to access the second value of it will crash because there is no second element. Try the following code, is should work...

          class myClass
          {
          // fields
          private string _name;
          private double[] _number = new double[5];

          // properties
          public string name
          {
          	get { return \_name; }
          	set { \_name = value; }
          }
          
          public double\[\] number
          {
          	get { return \_number; }
          	set { \_number = value; }
          }
          
          public double this\[int index\]
          {
          	get { return \_number\[index\]; }
          	set { \_number\[index\] = value; }
          }
          
          // Default constructor:
          public myClass()
          {
          	name = "";
          	\_number\[0\] = 0;
          	\_number\[1\] = 0;
          	\_number\[2\] = 0;
          	\_number\[3\] = 0;
          	\_number\[4\] = 0;
          }
          // Clear Numbers
          public void ClearNumbers()
          {
          	\_number\[0\] = 0;
          	\_number\[1\] = 0;
          	\_number\[2\] = 0;
          	\_number\[3\] = 0;
          	\_number\[4\] = 0;
          }
          

          }

          Hope that helps :) Cheers, Will H

          I 1 Reply Last reply
          0
          • K kasik

            You have two arrays, like Guffa said, number and _number and you dont assign anything to either of them. The fact that they have the same name, give or take an underscore, means nothing, they are completely different variables that are in no way related to each other. Your code crashes on x.number[1] because you havent initialised number to anything, and so it is currently null. So trying to access the second value of it will crash because there is no second element. Try the following code, is should work...

            class myClass
            {
            // fields
            private string _name;
            private double[] _number = new double[5];

            // properties
            public string name
            {
            	get { return \_name; }
            	set { \_name = value; }
            }
            
            public double\[\] number
            {
            	get { return \_number; }
            	set { \_number = value; }
            }
            
            public double this\[int index\]
            {
            	get { return \_number\[index\]; }
            	set { \_number\[index\] = value; }
            }
            
            // Default constructor:
            public myClass()
            {
            	name = "";
            	\_number\[0\] = 0;
            	\_number\[1\] = 0;
            	\_number\[2\] = 0;
            	\_number\[3\] = 0;
            	\_number\[4\] = 0;
            }
            // Clear Numbers
            public void ClearNumbers()
            {
            	\_number\[0\] = 0;
            	\_number\[1\] = 0;
            	\_number\[2\] = 0;
            	\_number\[3\] = 0;
            	\_number\[4\] = 0;
            }
            

            }

            Hope that helps :) Cheers, Will H

            I Offline
            I Offline
            IceWater42
            wrote on last edited by
            #5

            Thank you so VERY much, Will ! Your suggestions work great! I still do not understand the use of "this" in the code block ... public double this[int index] { get { return _number[index]; } set { _number[index] = value; } } ... but for the time being, until i get that understanding, i can mimic what u showed me for future class definitions. Your help is much appreciated!

            K 1 Reply Last reply
            0
            • I IceWater42

              Thank you so VERY much, Will ! Your suggestions work great! I still do not understand the use of "this" in the code block ... public double this[int index] { get { return _number[index]; } set { _number[index] = value; } } ... but for the time being, until i get that understanding, i can mimic what u showed me for future class definitions. Your help is much appreciated!

              K Offline
              K Offline
              kasik
              wrote on last edited by
              #6

              That code block is there so that you can access myClass as though it was an array itself. It means that if you create an instance of myClass like so

              myClass foo = new myClass();

              then you can access your _number array in two ways. Like this...

              double firstElement = foo.number[0];

              and also like this...

              double firstElement = foo[0];

              The last way is only allowed because of the 'public double this[int index]' accessor. Glad I could help :) Cheers, Will H

              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