Newbie here. Why is my array list not working?
-
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, " -
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, " -
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, "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 toListBoxResults
in yourPage_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
-
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 toListBoxResults
in yourPage_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
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.
-
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.
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 setterprivate
: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 fromPerson
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
-
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 setterprivate
: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 fromPerson
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
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 =)
-
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 =)
That would suggest that your
.aspx
markup file isn't correctly tied to your.aspx.cs
code-behind file. Check theCodeFile
/CodeBehind
andInherits
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
-
That would suggest that your
.aspx
markup file isn't correctly tied to your.aspx.cs
code-behind file. Check theCodeFile
/CodeBehind
andInherits
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