0

I want to add two lists to the box. It doesn't have a problem with adding items to the listbox, but a problem occurs when I try to click on one of the items and show a related form with details filled in to allow users to make amendments. Obviously the form didn't show, but instead an error occurs, no matter if I was try to open the form "delivery" or "pickup". The problem still occurs on one line

Error:

An unhandled exception of type 'System.InvalidCastException' occurred in coursework2.exe

Additional information: Unable to cast object of type 'System.String' to type 'coursework2.Pickup'.

namespace coursework2
{
    public partial class MainForm : Form
    {

        private DeliveryForm deliveryform = new DeliveryForm();
        private List<Visit> requests = new List<Visit>();
        private Visit theVisit = new Visit();
        private PickupForm pickupform = new PickupForm();




        public void requestVisit(Visit newVisit)
        {
            requests.Add(newVisit);
        }
        public MainForm()
        {
            InitializeComponent();
        }


        private void btnNpickup_Click(object sender, EventArgs e)
        {
           pickupform.pickup = new Pickup();
            pickupform.ShowDialog();
            if (pickupform.pickup != null)
            {
                Pickup newPu = pickupform.pickup;
                theVisit.addPick(newPu);

                List<String> listOfPic = theVisit.listofPicks();
                listboxVisits.Items.AddRange(listOfPic.ToArray());
            }
            updateList();
            //this will upload details from pickup form to the list
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void btnNdelivery_Click(object sender, EventArgs e)
        {
            deliveryform.delivery = new Delivery();
            deliveryform.ShowDialog();
            if (deliveryform.delivery != null)
            {
                Delivery newDe = deliveryform.delivery;
                theVisit.addDeliver(newDe);

                List<String> listOfDel = theVisit.listofDeliver();
                listboxVisits.Items.AddRange(listOfDel.ToArray());
            }
            updateList();
            //this will upload detail of the delivery to the list

        }

        private void btnVall_Click(object sender, EventArgs e)
        {

        }
        private void updateList()
        {
            listboxVisits.Items.Clear();
            List<String> listofVis = theVisit.LisitVisits();
            listboxVisits.Items.AddRange(listofVis.ToArray());


        }

        private void listboxVisits_SelectedIndexChanged(object sender, EventArgs e)
        {
            listboxVisits.FormattingEnabled = false;
            int index = listboxVisits.SelectedIndex;


                if (listboxVisits.SelectedItems.Count>0)
                {
                    object object1 = listboxVisits.SelectedItems[0];
                    if (object1 is Delivery)
                    {
                        Delivery deliver = (Delivery)object1;
                        deliveryform.delivery = deliver;
                        deliveryform.ShowDialog();
                    }

                    else
                    {
                        Pickup pick = (Pickup)object1;// this is where error occur
                        pickupform.pickup = pick;
                        pickupform.ShowDialog();
                    }
                }

this is the pickup class

public class Pickup
    {
        private string pickupname;
        private string pickupaddress;
        private Visit collects;
        private Delivery sends;
        private DateTime pdatetime;
        private string dname;
        private string daddress;
        private DateTime dtime;


        public string PickupName
        {
            get { return pickupname; }
            set { pickupname = value; }
        }

        public string PickupAddress
        {
            get { return pickupaddress; }
            set { pickupaddress = value; }
        }


        public string Dname
        {
            get { return dname; }
            set { dname = value; }
        }

        public string Daddress
        {
            get { return daddress; }
            set {daddress = value; }
        }

        public DateTime Pdatetime
        {
            get { return pdatetime; }
            set { pdatetime = value; }
        }

        public DateTime Dtime
        {
            get { return dtime; }
            set { dtime = value; }
        }

        public override string ToString()
        {
            return pickupname + " " + pickupaddress + " " + pdatetime.ToString()+" "+dname+" "+daddress+" "+dtime.ToString()+" Pickup ";
        }
    }

this is the visit class

 public class Visit : Customer
    {

        private Customer requester;
        private DateTime datetime;
        private Delivery reciever;
        private Pickup collect;

        public DateTime DateTime
        {
            get { return datetime; }
            set { datetime = value; }
        }


        private List<Pickup> picks = new List<Pickup>();

        private List<Visit> visits = new List<Visit>();
        private List<Delivery> deliver = new List<Delivery>();


        public void addDeliver(Delivery de)
        {
            //adding new Delivery ToString the visit
            deliver.Add(de);
        }



        public List<String> listofDeliver()
        {
            List<string> listofDeliver = new List<string>();

            foreach (Delivery de in deliver)
            {
                String deAsString = de.ToString();
                listofDeliver.Add(deAsString);
            }


            return listofDeliver;
        }

        public Delivery getDeliver(int index)
        {
            int count = 0;
            foreach (Delivery de in deliver)
            {
                if (index == count)
                    return de;
                count++;
            }
            return null;
        }

        public void addPick(Pickup pu)
        {
            picks.Add(pu);
        }


        public List<String> listofPicks()
        {
            List<string> listofPicks = new List<string>();

            foreach (Pickup pu in picks)
            {
                String puAsString = pu.ToString();
                listofPicks.Add(puAsString);
            }


            return listofPicks;
        }

        public Pickup getPicks(int index)
        {
            int count = 0;
            foreach (Pickup pu in picks)
            {
                if (index == count)
                    return pu;
                count++;
            }
            return null;
        }

        public List<String> LisitVisits()
        {
            List<String> visits = new List<string>();
            visits.AddRange(listofDeliver());
            visits.AddRange(listofPicks());
            return visits;
        }

        public Visit getVisits(int index)
        {
            int count = 0;
            foreach (Visit vis in visits)
            {
                if (index == count)
                    return vis;
                count++;
            }
            return null;
        }

        public string VisitDetails()
        {
            return collect.PickupName + " " + collect.PickupAddress + " Pickup " + reciever.DeliveryName + " " + reciever.DeliveryAddress + " Delivery";
        }
    }
user1211911
  • 203
  • 1
  • 2
  • 7
  • You populate listboxVisits with strings, so you retrieve a string back from the selected item. You can't just cast a string to a complex object. Perhaps use the retrieved string to do a lookup on theVisit to retrieve the correct Pickup object. It's hard to say exactly though without seeing the definition of the Visit class. – stevepkr84 Jan 08 '14 at 10:17
  • Short answer: you're holding a collection of strings in your listbox, but where the code fails you're expecting those contents to have somehow instead transformed into `Pickup` or `Delivery` types. If you need some help fixing your code, you'd need to post the contents of `Visit` class (in particular the `listOfPics()` and `listOfDeliver()` methods). You might also consider reading up on best practices in naming conventions for C# - especially if you want others to inspect your code. – decPL Jan 08 '14 at 10:21
  • am not sure if this helps.. but this is the visit class just posted it – user1211911 Jan 08 '14 at 10:24
  • You have gone down a bit of a dark path here tbh. You are returning a formatted string only to the listbox, which leaves you with having to use that string to try to do a lookup. I would suggest reading the post I link below, and populating the listbox with a string/id combination. Then you can retrieve the pickup/delivery objects using that ID and your existing getPicks / getDeliver methods: http://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source – stevepkr84 Jan 08 '14 at 10:32
  • sorry can you be more specific. to be honest i am quite stupid when is comes with coding.. and am kind of in urgent here. and i would really really appreciate.. – user1211911 Jan 08 '14 at 10:47

0 Answers0