Reputation: 77
So I run into the following problem. I have a textbox and listbox. Ive made a array like this:
string[] Brands = new string[10];
Brands[0] = "Yamaha";
Brands[1] = "Suzuki";
Brands[2] = "Harley";
Brands[3] = "Kawasaki";
The array has place for 10 values, but I have specified only four values. I want to add up to ten values into this array with a click on the button.
I can not use a list, it has to be an array. I already discovered that I need to place the above part in the
{
public partial class Form1 : Form
{
string[] Brands = new string[10];
Brands[0] = "Yamaha";
Brands[1] = "Suzuki";
Brands[2] = "Harley";
Brands[3] = "Kawasaki";
But it doens't recognize the Brands[].
The
private void buttonAddbrand_Click(object sender, EventArgs e)
{
Needs to contain the code for adding it into the array. Like refreshing the array.
I'm stuck, too much information on the internet and everybody suggests a list, but I need to use an array. Help would be much appreciated.
Upvotes: 3
Views: 1366
Reputation: 53
to the answare of Ian sure you can initialize this in this way why not?
string[] Brands = new string[10];
Brands[0] = "Yamaha";
Brands[1] = "Suzuki";
Brands[2] = "Harley";
Brands[3] = "Kawasaki";
but also in this way
string[] Brands = new string[10] { "Yamaha", "Suzuki", "Harley", "Kawasaki" };
coming back to you problem why not creating your own class which do'S whatever you like it to
public class Brands
{
#region private
private int _maxCapacity { get; set; }
private readonly List<string> _brands = new List<string>();
#endregion
#region Constructor
public Brands(int maxCapacity)
{
_maxCapacity = maxCapacity;
}
#endregion
#region public
// return all as array
public string[] AsArray
{
get { return _brands.ToArray(); }
}
/// <summary>
/// recive the actual count
/// </summary>
public int Count
{
get { return _brands.Count; }
}
/// <summary>
/// give item from position
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public string Get(int item)
{
return _brands.Count <= item ? _brands[item] : string.Empty;
}
/// <summary>
/// Set(Add) a value if enougth capacity
/// </summary>
/// <param name="value"></param>
public void Set(string value)
{
if (_brands.Count < _maxCapacity)
_brands.Add(value);
else
{
throw new ArgumentOutOfRangeException("Capacity");
}
}
/// <summary>
/// change an item
/// </summary>
/// <param name="item"></param>
/// <param name="value"></param>
public void Set(int item, string value)
{
if (_brands.Count <= item)
_brands[item] = value;
}
#endregion
}
// i did not compile it but this is the idea so in your form you simply use it i hope that helps
best regards
Upvotes: 1
Reputation: 30813
You cannot assign the array like this:
public partial class Form1 : Form
{
string[] Brands = new string[10];
Brands[0] = "Yamaha"; //fail
Brands[1] = "Suzuki"; //fail
Brands[2] = "Harley"; //fail
Brands[3] = "Kawasaki"; //fail
In the class context. Instead, you should do something like this
public partial class Form1 : Form
{
string[] Brands = new string[10] { "Yamaha", "Suzuki", "Harley", "Kawasaki", "", "", "", "", "", "" };
Then, if you declare Brands
as Form1
class
field, then in the
private void buttonAddbrand_Click(object sender, EventArgs e)
{
//Brands will be recognized
}
Brands will be recognized. If what you need to do is adding something to array (and you must use array). Then you should also preserve the number of the element in the current array like this:
public partial class Form1 : Form
{
string[] Brands = new string[10] { "Yamaha", "Suzuki", "Harley", "Kawasaki", "", "", "", "", "", "" };
int brandNo = 4;
Then, when you add new item to the array, do it like this:
private void buttonAddbrand_Click(object sender, EventArgs e)
{
if(brandNo >= 10)
return; //cannot add more brand
Brands[brandNo++] = "NewBrand"; //at the brandNo AFTER you put "NewBrand", see post-increment.
}
Edit:
As for the array initialization in the constructor, this is how your teacher wants - It is of longer method actually. But for the sake of teaching, I will just show it nevertheless:
public partial class Form1 : Form
{
string[] Brands = new string[10];
int brandNo; //This is a must
public Form1(){
InitializeComponent();
Brands[0] = "Yamaha"; //ok
Brands[1] = "Suzuki"; //ok
Brands[2] = "Harley"; //ok
Brands[3] = "Kawasaki"; //ok
brandNo = 4; //This is a must
}
Upvotes: 4
Reputation: 851
Ian's answer is a good one, it means you check if there is space and allows you to know where the space is.
An alternative, could be to not use an array, but instead use a list.
With a list you could do the following:
List<string> Brands = new List<string> { "Yamaha", "Suzuki", "Harley", "Kawasaki" };
Then in your add you can simply do:
private void buttonAddbrand_Click(object sender, EventArgs e)
{
if(Brands.Count >= 10)
return; //cannot add more brand
Brands.Add("NewBrand");
}
I believe (please someone correct me if I'm wrong) there is a slightly bigger memory overhead with a List but personally I prefer to use them over arrays where I can.
Upvotes: 2