Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Newbie here. Why is my array list not working?

Newbie here. Why is my array list not working?

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasedesigndata-structures
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marc Hede
    wrote on last edited by
    #1

    I am very new to ASP.net and C#, so I am trying my best, but there are stille many things that confuse me, especially because when I am looking at various examples online they give me different answers to the same solution, which just makes it even more confusing. I made a person class, with two subclasses (driver and admin), but I can not get the ArrayList to show up when I run my Index file. I only get a parse error. What do I need to change to make this work? My person class

    public class Person
    {
    public Person(string firstName, string lastName, int age, string email)
    {
    FirstName = firstName;
    LastName = lastName;
    Age = age;
    Email = email;
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string Email { get; }
    public virtual bool ChangeEmail(string email)
    {
    Email = email;
    return true;
    }
    public override string ToString()
    {
    return $"Name: {FirstName} {LastName}, Age: {Age}, E-mail: {Email}";
    }
    }

    My subclasses

    public class Driver : Person
    {
    private string v1;
    private string v2;
    private string v3;
    private string v4;
    private string v5;
    public Driver(string firstName, string lastName, int age, string email, int
    licenceNumber)
    : base(firstName, lastName, age, email)
    {
    LicenceNumber = licenceNumber;
    }
    public Driver(string v1, string v2, string v3, string v4, string v5)
    {
    this.v1 = v1;
    this.v2 = v2;
    this.v3 = v3;
    this.v4 = v4;
    this.v5 = v5;
    }
    public int LicenceNumber { get; set; }
    public override string ToString()
    {
    return $"Role: Traindriver, LicenceNumber: {LicenceNumber}, " + base.ToString();
    }
    }
    public class Admin : Person
    {
    public Admin(string firstName, string lastName, int age, string email)
    : base(firstName, lastName, age, email)
    {
    }
    public override bool ChangeEmail(string email)
    {
    if (!email.EndsWith("@pig.dk", StringComparison.InvariantCultureIgnoreCase))
    return false;
    return base.ChangeEmail(email);
    }
    public override string ToString()
    {
    return $"Role: Admin, " + base.ToString();
    }
    }
    }

    My ArrayList

    namespace Pig
    {
    public partial class Index : System.Web.UI.Page
    {
    public object ListBoxResults { get; private set; }
    public object DriverListBox { get; private set; }
    protected void Page_Load(object sender, EventArgs e)
    {
    Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
    Driver d2 = new Driver("Peter", "Jensen", 40, "

    P Richard DeemingR 2 Replies Last reply
    0
    • M Marc Hede

      I am very new to ASP.net and C#, so I am trying my best, but there are stille many things that confuse me, especially because when I am looking at various examples online they give me different answers to the same solution, which just makes it even more confusing. I made a person class, with two subclasses (driver and admin), but I can not get the ArrayList to show up when I run my Index file. I only get a parse error. What do I need to change to make this work? My person class

      public class Person
      {
      public Person(string firstName, string lastName, int age, string email)
      {
      FirstName = firstName;
      LastName = lastName;
      Age = age;
      Email = email;
      }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public int Age { get; set; }
      public string Email { get; }
      public virtual bool ChangeEmail(string email)
      {
      Email = email;
      return true;
      }
      public override string ToString()
      {
      return $"Name: {FirstName} {LastName}, Age: {Age}, E-mail: {Email}";
      }
      }

      My subclasses

      public class Driver : Person
      {
      private string v1;
      private string v2;
      private string v3;
      private string v4;
      private string v5;
      public Driver(string firstName, string lastName, int age, string email, int
      licenceNumber)
      : base(firstName, lastName, age, email)
      {
      LicenceNumber = licenceNumber;
      }
      public Driver(string v1, string v2, string v3, string v4, string v5)
      {
      this.v1 = v1;
      this.v2 = v2;
      this.v3 = v3;
      this.v4 = v4;
      this.v5 = v5;
      }
      public int LicenceNumber { get; set; }
      public override string ToString()
      {
      return $"Role: Traindriver, LicenceNumber: {LicenceNumber}, " + base.ToString();
      }
      }
      public class Admin : Person
      {
      public Admin(string firstName, string lastName, int age, string email)
      : base(firstName, lastName, age, email)
      {
      }
      public override bool ChangeEmail(string email)
      {
      if (!email.EndsWith("@pig.dk", StringComparison.InvariantCultureIgnoreCase))
      return false;
      return base.ChangeEmail(email);
      }
      public override string ToString()
      {
      return $"Role: Admin, " + base.ToString();
      }
      }
      }

      My ArrayList

      namespace Pig
      {
      public partial class Index : System.Web.UI.Page
      {
      public object ListBoxResults { get; private set; }
      public object DriverListBox { get; private set; }
      protected void Page_Load(object sender, EventArgs e)
      {
      Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
      Driver d2 = new Driver("Peter", "Jensen", 40, "

      P Offline
      P Offline
      phil o
      wrote on last edited by
      #2

      Please indicate the exact error message, as well as the line which is triggering it. "Not working" is not precise enough.

      "Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."

      1 Reply Last reply
      0
      • M Marc Hede

        I am very new to ASP.net and C#, so I am trying my best, but there are stille many things that confuse me, especially because when I am looking at various examples online they give me different answers to the same solution, which just makes it even more confusing. I made a person class, with two subclasses (driver and admin), but I can not get the ArrayList to show up when I run my Index file. I only get a parse error. What do I need to change to make this work? My person class

        public class Person
        {
        public Person(string firstName, string lastName, int age, string email)
        {
        FirstName = firstName;
        LastName = lastName;
        Age = age;
        Email = email;
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public string Email { get; }
        public virtual bool ChangeEmail(string email)
        {
        Email = email;
        return true;
        }
        public override string ToString()
        {
        return $"Name: {FirstName} {LastName}, Age: {Age}, E-mail: {Email}";
        }
        }

        My subclasses

        public class Driver : Person
        {
        private string v1;
        private string v2;
        private string v3;
        private string v4;
        private string v5;
        public Driver(string firstName, string lastName, int age, string email, int
        licenceNumber)
        : base(firstName, lastName, age, email)
        {
        LicenceNumber = licenceNumber;
        }
        public Driver(string v1, string v2, string v3, string v4, string v5)
        {
        this.v1 = v1;
        this.v2 = v2;
        this.v3 = v3;
        this.v4 = v4;
        this.v5 = v5;
        }
        public int LicenceNumber { get; set; }
        public override string ToString()
        {
        return $"Role: Traindriver, LicenceNumber: {LicenceNumber}, " + base.ToString();
        }
        }
        public class Admin : Person
        {
        public Admin(string firstName, string lastName, int age, string email)
        : base(firstName, lastName, age, email)
        {
        }
        public override bool ChangeEmail(string email)
        {
        if (!email.EndsWith("@pig.dk", StringComparison.InvariantCultureIgnoreCase))
        return false;
        return base.ChangeEmail(email);
        }
        public override string ToString()
        {
        return $"Role: Admin, " + base.ToString();
        }
        }
        }

        My ArrayList

        namespace Pig
        {
        public partial class Index : System.Web.UI.Page
        {
        public object ListBoxResults { get; private set; }
        public object DriverListBox { get; private set; }
        protected void Page_Load(object sender, EventArgs e)
        {
        Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
        Driver d2 = new Driver("Peter", "Jensen", 40, "

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        You have a control with the ID ListBoxResults, which will automatically generate a field in your class:

        private ListBox ListBoxResults;

        You have also declared a property called ListBoxResults in your code-behind. I'd be extremely surprised if that code compiled; if it did, your references to ListBoxResults in your Page_Load method would be referring to the property, not the control. Remove the property and your code should start working:

        namespace Pig
        {
        public partial class Index : System.Web.UI.Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {
        Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
        Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
        Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
        ListBoxResults.Items.Add(d1.ToString());
        ListBoxResults.Items.Add(d2.ToString());
        ListBoxResults.Items.Add(a1.ToString());
        }
        }
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        M 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You have a control with the ID ListBoxResults, which will automatically generate a field in your class:

          private ListBox ListBoxResults;

          You have also declared a property called ListBoxResults in your code-behind. I'd be extremely surprised if that code compiled; if it did, your references to ListBoxResults in your Page_Load method would be referring to the property, not the control. Remove the property and your code should start working:

          namespace Pig
          {
          public partial class Index : System.Web.UI.Page
          {
          protected void Page_Load(object sender, EventArgs e)
          {
          Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
          Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
          Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
          ListBoxResults.Items.Add(d1.ToString());
          ListBoxResults.Items.Add(d2.ToString());
          ListBoxResults.Items.Add(a1.ToString());
          }
          }
          }


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          Marc Hede
          wrote on last edited by
          #4

          Okay, I removed the property and also changed "ListBoxResults" to simply "ListBox". There are some problems remaining. I will list the errors here (sorry for not doing so at first)

                  ListBox.Items.Add(d1.ToString());
                  ListBox.Items.Add(d2.ToString());
                  ListBox.Items.Add(a1.ToString());
          

          Here I get the error: "An object reference is required for the non-static field, method, or property." If I remember correctly, a solution to this would be to make a static ArrayList, but I am not sure how I can do it for this code. Error no. 2

          public virtual bool ChangeEmail(string email)
          {
          Email = email;
          return true;
          }

          Error: "Property or indexer cannot be assigned to -- it is read only"

          Error no. 3

              public Driver(string v1, string v2, string v3, string v4, string v5)
              {
                  this.v1 = v1;
                  this.v2 = v2;
                  this.v3 = v3;
                  this.v4 = v4;
                  this.v5 = v5;
              }
          

          Error message: There is no argument given that corresponds to the required formal parameter of 'firstName' of 'Person.Person (string, string, int, string)'

          Thanks a ton for being patient with me, you guys.

          Richard DeemingR 1 Reply Last reply
          0
          • M Marc Hede

            Okay, I removed the property and also changed "ListBoxResults" to simply "ListBox". There are some problems remaining. I will list the errors here (sorry for not doing so at first)

                    ListBox.Items.Add(d1.ToString());
                    ListBox.Items.Add(d2.ToString());
                    ListBox.Items.Add(a1.ToString());
            

            Here I get the error: "An object reference is required for the non-static field, method, or property." If I remember correctly, a solution to this would be to make a static ArrayList, but I am not sure how I can do it for this code. Error no. 2

            public virtual bool ChangeEmail(string email)
            {
            Email = email;
            return true;
            }

            Error: "Property or indexer cannot be assigned to -- it is read only"

            Error no. 3

                public Driver(string v1, string v2, string v3, string v4, string v5)
                {
                    this.v1 = v1;
                    this.v2 = v2;
                    this.v3 = v3;
                    this.v4 = v4;
                    this.v5 = v5;
                }
            

            Error message: There is no argument given that corresponds to the required formal parameter of 'firstName' of 'Person.Person (string, string, int, string)'

            Thanks a ton for being patient with me, you guys.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Member 14671427 wrote:

            and also changed "ListBoxResults" to simply "ListBox"

            Which is not what I said, and is the cause of the "object reference required" errors. Remove the property; leave the Page_Load method as I showed you:

            namespace Pig
            {
            public partial class Index : System.Web.UI.Page
            {
            protected void Page_Load(object sender, EventArgs e)
            {
            Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
            Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
            Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
            ListBoxResults.Items.Add(d1.ToString());
            ListBoxResults.Items.Add(d2.ToString());
            ListBoxResults.Items.Add(a1.ToString());
            }
            }
            }

            Member 14671427 wrote:

            Error: "Property or indexer cannot be assigned to -- it is read only"

            You haven't added a setter to the Email property, so you can't set it. If you don't want to be able to change it from outside of the class, then make the setter private:

            public string Email { get; private set; }

            Member 14671427 wrote:

            There is no argument given that corresponds to the required formal parameter of 'firstName' of 'Person.Person (string, string, int, string)'

            Your Person constructor requires four arguments. Every constructor of a class derived from Person must pass those four arguments into the base constructor.

            public Driver(string v1, string v2, string v3, string v4, string v5)
            : base("??? FIRST NAME ???", "??? LAST NAME ???", 0, "??? EMAIL ???")


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            M 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Member 14671427 wrote:

              and also changed "ListBoxResults" to simply "ListBox"

              Which is not what I said, and is the cause of the "object reference required" errors. Remove the property; leave the Page_Load method as I showed you:

              namespace Pig
              {
              public partial class Index : System.Web.UI.Page
              {
              protected void Page_Load(object sender, EventArgs e)
              {
              Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
              Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
              Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
              ListBoxResults.Items.Add(d1.ToString());
              ListBoxResults.Items.Add(d2.ToString());
              ListBoxResults.Items.Add(a1.ToString());
              }
              }
              }

              Member 14671427 wrote:

              Error: "Property or indexer cannot be assigned to -- it is read only"

              You haven't added a setter to the Email property, so you can't set it. If you don't want to be able to change it from outside of the class, then make the setter private:

              public string Email { get; private set; }

              Member 14671427 wrote:

              There is no argument given that corresponds to the required formal parameter of 'firstName' of 'Person.Person (string, string, int, string)'

              Your Person constructor requires four arguments. Every constructor of a class derived from Person must pass those four arguments into the base constructor.

              public Driver(string v1, string v2, string v3, string v4, string v5)
              : base("??? FIRST NAME ???", "??? LAST NAME ???", 0, "??? EMAIL ???")


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              M Offline
              M Offline
              Marc Hede
              wrote on last edited by
              #6

              Hi again and thanks once again. I did do ask you said. The reason why I tried playing around with the ListBox line is because I am getting an CS0103 error: "The name ListBoxResults does not exist in the current context", which I don't get, because I looked in my index file. It says

              I checked if the namespace was the issue, but that was not that case. However, the object reference error is gone, just like you said =)

              Richard DeemingR 1 Reply Last reply
              0
              • M Marc Hede

                Hi again and thanks once again. I did do ask you said. The reason why I tried playing around with the ListBox line is because I am getting an CS0103 error: "The name ListBoxResults does not exist in the current context", which I don't get, because I looked in my index file. It says

                I checked if the namespace was the issue, but that was not that case. However, the object reference error is gone, just like you said =)

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                That would suggest that your .aspx markup file isn't correctly tied to your .aspx.cs code-behind file. Check the CodeFile / CodeBehind and Inherits attributes on the <%@ Page ... %> directive in your markup file. @ Page | Microsoft Docs[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                M 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  That would suggest that your .aspx markup file isn't correctly tied to your .aspx.cs code-behind file. Check the CodeFile / CodeBehind and Inherits attributes on the <%@ Page ... %> directive in your markup file. @ Page | Microsoft Docs[^]


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  M Offline
                  M Offline
                  Marc Hede
                  wrote on last edited by
                  #8

                  Ah, what an idiot I was. In Inhereits, I had written "index" instead of "Index". Thanks a lot for helping me =)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups