title: How can I add "item" to arraylist, if checkbox is marked?
body: <p>I'm still kinda new into c#, and I'm currently stucked in a project. So basically I've currently have a form consisting of 5 textboxes, one checkbox and a button. When the user fills in the information, and submitting without marking the checkbox, the information is stored in a arraylist. If the user signs up as a traindriver, and marks the traindriver checkbox and submits, then a "id" need to be passed in to the arraylist, aswell as all the other information like first name and last name etc. </p>
  
  <p>Currently I have a person class, which works just fine. Then I have a subclass called traindrivers, which one consists of a "id" variable, that "increments" every time a new traindrivers is signed up, since they can't have the same number. So how can I add this "id" to the "personsArrayList" if the person that are signed up, are marked as a traindriver?</p>
  
  <p><a href="https://i.stack.imgur.com/qbKgw.png" rel="nofollow noreferrer">Form</a></p>
  
  <p>I've tried to use the procedure as I've used when new Persons are added. But for some reason, I couldn't find the varaibles which contains the id. </p>
  
  <pre><code>public class Persons
  {
      //instance variables
      private string firstname;
      private string lastname;
      private string email;
      private int age;
  
  
      //constructor
      public Persons(string firstname, string lastname, string email, int age)
      {
          this.firstname = firstname;
          this.lastname = lastname;
          this.email = email;
          this.age = age;
      }
  
      public string FirstName
      {
          get { return firstname; }
          set { firstname = value; }
      }
      public string LastName
      {
          get { return lastname; }
          set { lastname = value; }
      }
      public string Email
      {
          get { return email; }
          set { email = value; }
      }
      public int Age
      {
          get { return age; }
          set { age = value; }
      }
  
      public override string ToString()
      {
          return FirstName + " " + LastName + " - " + Email + " - " + Age + ".";
      }
  
  }
  
  public class Traindrivers : Persons
  {
      protected int id;
      protected static int mycounter;
  
      public Traindrivers(int id, string firstname, string lastname, string email, int age) : base(firstname, lastname, email, age)
      {
          mycounter++;
          id = mycounter;
      }
  
  
      public int LicensNumber
      {
          get { return id; }
          set { id = value; }
      }
  
      public override string ToString()
      {
          return LicensNumber + FirstName + LastName + Email + Age;
      }
  }
  </code></pre>
  
  <p>----------- aspx page ---------</p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
      {
          Persons p1 = new Persons(TextBoxFirstName.Text, TextBoxLastName.Text, TextBoxEmail.Text, Convert.ToInt32(TextBoxAge.Text));
          personsArrayList.Add(p1);
  
          //Session["trainpersons"] = personsArrayList;
      }
  
      protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
      {
        // When the checkbox is marked, add "id" to the arraylist
      }
  
      protected void ShowButton_Click(object sender, EventArgs e)
      {
          ListBox1.Items.Clear();
          for (int i = 0; i &lt; personsArrayList.Count; i++)
          {
              ListBox1.Items.Add(personsArrayList[i].ToString());
              Console.WriteLine(i);
          }
      }
  }
  
  protected void trainBtn_Click(object sender, EventArgs e)
      {
          ListBox1.Items.Clear();
          for (int i = 0; i &lt; traindriversArrayList.Count; i++)
          {
              ListBox1.Items.Add(traindriversArrayList[i].ToString());
          }
      }
  </code></pre>
  
  <p>The expected result is to add "id" whenever is it a traindriver, that signs up. And it is of course expected to go into the personsArrayList.</p>