I want to create an List<> of an object to store Guest's base information and have some codes like that
private void btnSave_Click(object sender, EventArgs e)
{
int gender;
string fn;
string ln;
string num;
int bd;
int bm;
int by;
IList<GuestInfo> GuestList = new List<GuestInfo>();
if(tbBirthDate.Text==null||tbBirthMonth==null||tbBirthYear==null||tbFname==null||tbLName==null||tbPhoneNum==null)
{
MessageBox.Show("Please fill in all information");
}
else if (count>=4)
{
MessageBox.Show("You already have 4 members infomation save");
}
else
{
if(radFemale.Checked == true)
{
gender = 0;
}
else
{
gender = 1;
}
fn = tbFname.Text;
ln = tbLName.Text;
num = tbPhoneNum.Text;
bd = Int32.Parse(tbBirthDate.Text);
bm = Int32.Parse(tbBirthMonth.Text);
by = Int32.Parse(tbBirthYear.Text);
GuestList.Add(new GuestInfo(fn,ln,num,gender,bd,bm,by));
}
}
}
GuestInfo:
class GuestInfo
{
private string fName;
private string lName;
private string pNum;
private int Gen;
private int bDate;
private int bMonth;
private int bYear;
public GuestInfo(string FirstName,string LastName, string phoneNum,int Gender, int birthDate,int birthMonth,int birthYear)
{
fName = FirstName;
lName = LastName;
pNum = phoneNum;
Gen = Gender;
bDate = birthDate;
bMonth = birthDate;
bYear = birthYear;
}
}
When the button clicked I store the information, but I don't know how to get a data I wanna use, such as I want to know the First name of Guest on GuestList[1].
.afterGuestList[1]GuestList[1]since you are declaring and initializing the List in the event itself. This object will be destroyed soon after your event is fired and you exit. So first thing is you need to create theList<GuestInfo>at the top after your form class. Then comes the question of fetching.