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. Create array in class.

Create array in class.

Scheduled Pinned Locked Moved C#
csharplinqgraphicsdata-structurestutorial
6 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.
  • H Offline
    H Offline
    Herboren
    wrote on last edited by
    #1

    Im trying to figure out how to create an array in my class. What I have now is:

    namespace Computer_Specs
    {
    class Computer
    {
    private string _Brand;
    private int _RAM;
    private string _cpuBrand;
    private float _cpuSpeed;
    private string _operatingSystem;

        //Array
        private string\[\] \_clArray = new string\[5\];
        public string brand
        {
            get { return this.\_Brand; }
            set { this.\_Brand = value; }
        }
        public int ram
        {
            get { return this.\_RAM; }
            set { this.\_RAM = value; }
        }
        public string cpuBrand
        {
            get { return this.\_cpuBrand; }
            set { this.\_cpuBrand = value; }
        }
        public float cpuSpeed
        {
            get { return this.\_cpuSpeed; }
            set { this.\_cpuSpeed = value; }
        }
        public string os
        {
            get { return this.\_operatingSystem; }
            set { this.\_operatingSystem = value; }
        }
        //Array
        public string\[\] cla
        {
            get { return this.\_clArray; }
            set { this.\_clArray = value; }
        }
    }
    

    } //Array
    private string[] _clArray = new string[5];

        //Array
        public string\[\] cla
        {
            get { return this.\_clArray; }
            set { this.\_clArray = value; }
        }
    

    But within my main form I don't know how to actually use the object to fill the array.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace Computer_Specs
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
    string nl = Environment.NewLine;
    Computer specs = new Computer();

            tbLog.Text = specs.brand = tbBrand.Text + nl;
            tbLog.Text = tbLog.Text + (specs.ram = int.Parse(tbRAM.Text)) + nl;
            tbLog.Text = tbLog.Text + (specs.cpuBrand = tbcpuBrand.Text) + nl;
            tbLog.Text = tbLog.Text + (specs.cpuSpeed = float.Parse(tbCPUSpeed.Text)) + nl;
            tbLog.Text = tbLog.Text + (specs.os = tbOS.Text) + nl;
            
            //Right here is whe
    
    U 1 Reply Last reply
    0
    • H Herboren

      Im trying to figure out how to create an array in my class. What I have now is:

      namespace Computer_Specs
      {
      class Computer
      {
      private string _Brand;
      private int _RAM;
      private string _cpuBrand;
      private float _cpuSpeed;
      private string _operatingSystem;

          //Array
          private string\[\] \_clArray = new string\[5\];
          public string brand
          {
              get { return this.\_Brand; }
              set { this.\_Brand = value; }
          }
          public int ram
          {
              get { return this.\_RAM; }
              set { this.\_RAM = value; }
          }
          public string cpuBrand
          {
              get { return this.\_cpuBrand; }
              set { this.\_cpuBrand = value; }
          }
          public float cpuSpeed
          {
              get { return this.\_cpuSpeed; }
              set { this.\_cpuSpeed = value; }
          }
          public string os
          {
              get { return this.\_operatingSystem; }
              set { this.\_operatingSystem = value; }
          }
          //Array
          public string\[\] cla
          {
              get { return this.\_clArray; }
              set { this.\_clArray = value; }
          }
      }
      

      } //Array
      private string[] _clArray = new string[5];

          //Array
          public string\[\] cla
          {
              get { return this.\_clArray; }
              set { this.\_clArray = value; }
          }
      

      But within my main form I don't know how to actually use the object to fill the array.

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      namespace Computer_Specs
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }
      private void button1_Click(object sender, EventArgs e)
      {
      string nl = Environment.NewLine;
      Computer specs = new Computer();

              tbLog.Text = specs.brand = tbBrand.Text + nl;
              tbLog.Text = tbLog.Text + (specs.ram = int.Parse(tbRAM.Text)) + nl;
              tbLog.Text = tbLog.Text + (specs.cpuBrand = tbcpuBrand.Text) + nl;
              tbLog.Text = tbLog.Text + (specs.cpuSpeed = float.Parse(tbCPUSpeed.Text)) + nl;
              tbLog.Text = tbLog.Text + (specs.os = tbOS.Text) + nl;
              
              //Right here is whe
      
      U Offline
      U Offline
      User 4483848
      wrote on last edited by
      #2

      List claData = new List();

      claData.Add("Test1");
      claData.Add("Test2");
      claData.Add("etc.");

      specs.cla = claData.ToArray();

      I've just realised that you've initialised your arrays to 5 elements. This probably isn't a good idea. Whatever is using the array probably won't know it has five elements. Were you expecting something to do

      specs.clas[0] = "some text"

      ? In this case, you can access all elements of the array (and change them) from outside the class.

      H 1 Reply Last reply
      0
      • U User 4483848

        List claData = new List();

        claData.Add("Test1");
        claData.Add("Test2");
        claData.Add("etc.");

        specs.cla = claData.ToArray();

        I've just realised that you've initialised your arrays to 5 elements. This probably isn't a good idea. Whatever is using the array probably won't know it has five elements. Were you expecting something to do

        specs.clas[0] = "some text"

        ? In this case, you can access all elements of the array (and change them) from outside the class.

        H Offline
        H Offline
        Herboren
        wrote on last edited by
        #3

        I tested it and it worked!

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        namespace Computer_Specs
        {
        class Computer
        {
        private string _Brand;
        private int _RAM;
        private string _cpuBrand;
        private float _cpuSpeed;
        private string _operatingSystem;
        //Array
        private string[] _clArray;
        public string brand
        {
        get { return this._Brand; }
        set { this._Brand = value; }
        }
        public int ram
        {
        get { return this._RAM; }
        set { this._RAM = value; }
        }
        public string cpuBrand
        {
        get { return this._cpuBrand; }
        set { this._cpuBrand = value; }
        }
        public float cpuSpeed
        {
        get { return this._cpuSpeed; }
        set { this._cpuSpeed = value; }
        }
        public string os
        {
        get { return this._operatingSystem; }
        set { this._operatingSystem = value; }
        }
        //Array
        public string[] cla
        {
        get { return this._clArray; }
        set { this._clArray = value; }
        }
        }
        }

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        namespace Computer_Specs
        {
        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
        string nl = Environment.NewLine;
        Computer specs = new Computer();

                tbLog.Text = specs.brand = tbBrand.Text + nl;
                tbLog.Text = tbLog.Text + (specs.ram = int.Parse(tbRAM.Text)) + nl;
                tbLog.Text = tbLog.Text + (specs.cpuBrand = tbcpuBrand.Text) + nl;
                tbLog.Text = tbLog.Text + (specs.cpuSpeed = float.Parse(tbCPUSpeed.Text)) + nl;
                tbLog.Text = tbLog.Text + (specs.os = tbOS.Text) + nl + nl;
                List claData = new List();
                int loopY = 0;
                while (loopY < 5)
                {
                    claData.Add(loopY.ToString());
                    loopY++;
                } specs.cla = claData.ToArray();
                int loopX = 0;
                while (loopX < 5)
                {
                    t
        
        K 1 Reply Last reply
        0
        • H Herboren

          I tested it and it worked!

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          namespace Computer_Specs
          {
          class Computer
          {
          private string _Brand;
          private int _RAM;
          private string _cpuBrand;
          private float _cpuSpeed;
          private string _operatingSystem;
          //Array
          private string[] _clArray;
          public string brand
          {
          get { return this._Brand; }
          set { this._Brand = value; }
          }
          public int ram
          {
          get { return this._RAM; }
          set { this._RAM = value; }
          }
          public string cpuBrand
          {
          get { return this._cpuBrand; }
          set { this._cpuBrand = value; }
          }
          public float cpuSpeed
          {
          get { return this._cpuSpeed; }
          set { this._cpuSpeed = value; }
          }
          public string os
          {
          get { return this._operatingSystem; }
          set { this._operatingSystem = value; }
          }
          //Array
          public string[] cla
          {
          get { return this._clArray; }
          set { this._clArray = value; }
          }
          }
          }

          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Windows.Forms;
          namespace Computer_Specs
          {
          public partial class Form1 : Form
          {
          public Form1()
          {
          InitializeComponent();
          }
          private void button1_Click(object sender, EventArgs e)
          {
          string nl = Environment.NewLine;
          Computer specs = new Computer();

                  tbLog.Text = specs.brand = tbBrand.Text + nl;
                  tbLog.Text = tbLog.Text + (specs.ram = int.Parse(tbRAM.Text)) + nl;
                  tbLog.Text = tbLog.Text + (specs.cpuBrand = tbcpuBrand.Text) + nl;
                  tbLog.Text = tbLog.Text + (specs.cpuSpeed = float.Parse(tbCPUSpeed.Text)) + nl;
                  tbLog.Text = tbLog.Text + (specs.os = tbOS.Text) + nl + nl;
                  List claData = new List();
                  int loopY = 0;
                  while (loopY < 5)
                  {
                      claData.Add(loopY.ToString());
                      loopY++;
                  } specs.cla = claData.ToArray();
                  int loopX = 0;
                  while (loopX < 5)
                  {
                      t
          
          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          Your class looks pretty good, here are some suggestions to tighten it up:

          string[] _clArray;

          Don't have members with typenames, this should be _cl (or _cla if the a means something other than array). For one thing it is redundant, secondly it is against naming conventions, thirdly names should be meaningful (using array might make sense in an type classing a solar panel array for example). The Microsoft guidance is not to prefix fields (with "_" for example), but I (and most of the people I know do this, as it is internal other devs won't see it (unless reading your code directly). You should consider:

          public string[] Cla
          {
          get { return this._clArray; }
          private set { this._clArray = value; }
          }

          private prevents the array instance being changed from outide the class. It does lead to problems filling the array, you can do this with the class's constructor or with a specific array-setting method. Personally I'd go for the generic List<string> mentioned earlier by member_nnnnnnn_. With this you just create the instance inside the object and then can call the it's add method from the get accesssor. Under various circumstances you might want to make the list totally immutable from outside the class, to do this you should get rid of the public accessor you have and look at collection as readonly[^], you can then add Add and Remove methods etc that perform some validation for example. Note also Cla your class breaks the normal .net naming conventions, you should try to keep to these: the Microsoft classes are written this way and you will confuse other devs using your code by not doing this.

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]
          -Or-
          A Dead ringer for Kate Winslett[^]

          B 1 Reply Last reply
          0
          • K Keith Barrow

            Your class looks pretty good, here are some suggestions to tighten it up:

            string[] _clArray;

            Don't have members with typenames, this should be _cl (or _cla if the a means something other than array). For one thing it is redundant, secondly it is against naming conventions, thirdly names should be meaningful (using array might make sense in an type classing a solar panel array for example). The Microsoft guidance is not to prefix fields (with "_" for example), but I (and most of the people I know do this, as it is internal other devs won't see it (unless reading your code directly). You should consider:

            public string[] Cla
            {
            get { return this._clArray; }
            private set { this._clArray = value; }
            }

            private prevents the array instance being changed from outide the class. It does lead to problems filling the array, you can do this with the class's constructor or with a specific array-setting method. Personally I'd go for the generic List<string> mentioned earlier by member_nnnnnnn_. With this you just create the instance inside the object and then can call the it's add method from the get accesssor. Under various circumstances you might want to make the list totally immutable from outside the class, to do this you should get rid of the public accessor you have and look at collection as readonly[^], you can then add Add and Remove methods etc that perform some validation for example. Note also Cla your class breaks the normal .net naming conventions, you should try to keep to these: the Microsoft classes are written this way and you will confuse other devs using your code by not doing this.

            Sort of a cross between Lawrence of Arabia and Dilbert.[^]
            -Or-
            A Dead ringer for Kate Winslett[^]

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            "It does lead to problems filling the array" No it doesn't, arr[i] only uses the get indexer. As you point out later, this is sometimes not what you want and you have to provide a wrapper. Generally an excellent post though.

            K 1 Reply Last reply
            0
            • B BobJanova

              "It does lead to problems filling the array" No it doesn't, arr[i] only uses the get indexer. As you point out later, this is sometimes not what you want and you have to provide a wrapper. Generally an excellent post though.

              K Offline
              K Offline
              Keith Barrow
              wrote on last edited by
              #6

              You are quite right, I wasn't at all clear. It would have been better put "You then have the problem of instantiating the array to the correct size for the items you are going to fill it with" which means you need to know how many items you are going to have at the ctor call, or provide a method to instantiate an array of the correct size when you do know.

              Sort of a cross between Lawrence of Arabia and Dilbert.[^]
              -Or-
              A Dead ringer for Kate Winslett[^]

              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