سلام استاد
خدا قوت
من این کد رو نوشتم اما موقع اجرا خطای خارج بودن محدوده ی index ها از محدوده ی آرایه رو برمیگردونه
البته در ابتدا برای کلاس databais یه سازنده ایجاد کردم که مقدار count رو دریافت میکرد اما بعد از خط
DataBais databais=new DataBais[Count]
زمانی که میخواستم به مقدار count دسترسی داشته باشم،count رو بهم نشون نمیداد،بخاطر همین برنامه رو به این شکل که واستون ارسال کردم تغییر دادم.
internal class DataBais
{
public static int index = 0;
public static int Count;
//public DataBais(int count)
// {
// Count = count;
// }
public static Person[] list = new Person[Count];
public static void nextIndex()
{ index++; }
}
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
public void ChangeComboType()
{
switch (comboBox1.SelectedIndex)
{
case 0:
{
DataBais.Count = 5;
break;
}
case 1:
{
DataBais.Count = 10;
break;
}
case 2:
{
DataBais.Count = 15;
break;
}
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
ChangeComboType();
//DataBais databais=new DataBais[Count]
frmAdd frmAdd = new frmAdd();
frmAdd.ShowDialog();
dataGridView1.Rows.Clear();
foreach (Person p in DataBais.list)
{
if (p != null)
{
dataGridView1.Rows.Add(p.FirstName, p.Family, p.Email, p.Mobile, p.Type);
}
}
if (dataGridView1.RowCount > DataBais.list.Length)
{
btnAdd.Enabled = false;
MessageBox.Show("ظرفیت لیست پر شده است.");
}
}
private void frmMain_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
btnAdd.Enabled = true;
}
}
public partial class frmAdd : Form
{
public frmAdd()
{
InitializeComponent();
}
private void btnsabt_Click(object sender, EventArgs e)
{
if (txtname.Text != "" && txtfamily.Text != "")
{
Person person = new Person()
{
FirstName = txtname.Text,
Family = txtfamily.Text
};
person.Email = txtemail.Text;
person.Mobile = txtmobile.Text;
if (rdstudent.Checked)
{
person.Type = "Student";
}
else if (rdkarmand.Checked)
{
person.Type = "Employee";
}
else
{
person.Type = "Teacher";
}
DataBais.list[DataBais.index] = person;
DataBais.nextIndex();
this.Close();
}
else
{
MessageBox.Show("نوشتن نام و نام خانوادگی اجباری است!");
}
}
متن خطا کاملا مشخصه
در شماره اندیس ها اشتباه کردید
تمرین بقیه دوستان رو هم ببینید