0

I'm trying to access a List within an ArrayList to bind that list to a listbox.

Here is the code for my driver class.

public class Driver
{
    private string name;
    private string occupation;
    private DateTime dateOfBirth;
    private List<DateTime> dateOfClaim;

    public Driver(string name, string occupation, DateTime dateOfBirth, List<DateTime> dateOfClaim)
    {
        this.name = name;
        this.occupation = occupation;
        this.dateOfBirth = dateOfBirth;
        this.dateOfClaim = new List<DateTime>();
    }

    public string driverName
    {
        get{ return name; }
        set{ name = value; }
    }

    public string driverOccupation
    {
        get { return occupation; }
        set { occupation = value; }
    }

    public DateTime driverDateOfBirth
    {
        get { return dateOfBirth; }
        set { dateOfBirth = value; }
    }

    public List<DateTime> driverDateOfClaim
    {
        get { return dateOfClaim; }
        set { dateOfClaim = value; }
    }
}

I have a button which allows you to add a date for a claim to a driver, up to a maximum of 5 claims so I have a temporary list which holds the dates list before it is assigned to the list in the arraylist as the new driver has not yet been created in the arraylist.

Here are parts of the code from a form which declares and populates the arraylist.

private ArrayList drivers = new ArrayList();
private List<DateTime> claimDates = new List<DateTime>();

if (noOfClaims <= 4)
{
    claimDates.Add(dtpAddClaim.Value.Date);
    noOfClaims++;
}

if (noOfDrivers <= 4)
{
    drivers.Add(new Driver(txtName.Text, txtOccupation.Text, dtpDOB.Value.Date, claimDates));
    noOfDrivers++;
}

So my problem comes when trying to access the dateOfClaim list.

I'm trying to bind that list to a listbox and have tried using this to do that but am getting an error saying 'ArrayList' does not contain a definition for 'driverDateOfClaim'.

lbxClaimDates.DataSource = drivers.driverDateOfClaim;

Any help solving this would be greatly appreciated.

4
  • drivers contains a list of drivers, not a single driver. Which elements date of claims are you trying to retrieve? Commented Dec 5, 2015 at 16:39
  • 2
    Why don't you use a List<Driver> instead of an ArrayList? Commented Dec 5, 2015 at 16:40
  • I've tried using both, having the same problems with either. Commented Dec 5, 2015 at 17:40
  • There are two things out there only for backwards compatibility - collection and arraylist. for new dev you shouldn't use them Commented Dec 5, 2015 at 22:24

1 Answer 1

1

You need to index the array list. You would need to access an item of the array list and then acces driverDateOfClaim for a specific Driver instance in the ArrayList

lbxClaimDates.DataSource = ((Driver) drivers[0]).driverDateOfClaim 

Also in Drivers constructor you arent assigning the dateOfClaim parameter to the DateOfClaim property.

Sign up to request clarification or add additional context in comments.

6 Comments

"driverDateOfClaim" is a List<DateTime>.
Yes but the binding needs to be done to a specific element of the arraylist in order to access driverDateOfClaim property within the Driver instance
I've tried what you suggested but am still getting the "'object' does not contain a definition for 'driverDateOfClaim'" error. I also fixed the driver constructor, not sure why I had it like that.
I think that might be because ArrayList holds a list of Object types. so you need to cast it to a Driver First. Ill edit my post with a cast. I have used the 0th index as an exmaple but you would have to get the right driver from the the ArrayList you need
With the updated cast I'm no longer getting an error, but the listbox isn't displaying anything. Is there something I'm missing when it comes to displaying items in a listbox?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.