Create array in class.
-
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
-
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
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.
-
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.
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
-
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
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 genericList<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 addAdd
andRemove
methods etc that perform some validation for example. Note alsoCla
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[^] -
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 genericList<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 addAdd
andRemove
methods etc that perform some validation for example. Note alsoCla
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[^] -
"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.
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[^]