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. Correction: How to define an array in a class

Correction: How to define an array in a class

Scheduled Pinned Locked Moved C#
helpdatabasedata-structurestutorial
4 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 corrected my original post. I hope someone can help me with what should be a simple problem. ------------------------------------------------------- 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.

    N G 2 Replies Last reply
    0
    • I IceWater42

      Hi ... I corrected my original post. I hope someone can help me with what should be a simple problem. ------------------------------------------------------- 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.

      N Offline
      N Offline
      Nicholas Butler
      wrote on last edited by
      #2

      Hi Your first problem is this line:

      public double[] number;

      You are declaring a public field called number. What you want is:

      public double[] number { get { return _number; } }

      You have made another mistake twice - in your constructor and your ClearNumbers method. This line declares a local variable called number:

      double[] number = new double[5];

      What you want is:

      _number = new double[5];

      ---------------------------- Be excellent to each other :) EasiReports[^] My free reporting component for WinForms.

      I 1 Reply Last reply
      0
      • N Nicholas Butler

        Hi Your first problem is this line:

        public double[] number;

        You are declaring a public field called number. What you want is:

        public double[] number { get { return _number; } }

        You have made another mistake twice - in your constructor and your ClearNumbers method. This line declares a local variable called number:

        double[] number = new double[5];

        What you want is:

        _number = new double[5];

        ---------------------------- Be excellent to each other :) EasiReports[^] My free reporting component for WinForms.

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

        Thank you VERY VERY VERY much ... it works great! Now i know how to do it in my real application. Once again, thanks for your help!

        1 Reply Last reply
        0
        • I IceWater42

          Hi ... I corrected my original post. I hope someone can help me with what should be a simple problem. ------------------------------------------------------- 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.

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

          Why are you starting a new thread? Keep it in the same thread. --- b { font-weight: normal; }

          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