0

I am trying to figure out a way to search and check an array while filling it up. I want to use the overloaded equals method I have created from another class.

Here is the overloaded equals method in the public class Order : IComparable

public override bool Equals(object obj)
    {

            Order temp = (Order)obj;
        if (orderNumber == temp.orderNumber)
            return true;
        else 
            return false;

    }

in main, I try to create the 2 arrays so I can compare 1 to the other and check for dups as I go through with a nested for loop but it is not working. It just continues filling the array as if it never saw the second for loop

public static void Main()

    {
    //Create an array of five ShippedOrder objects. 
        ShippedOrder[] myShippedOrder = new ShippedOrder[5];
        ShippedOrder[] checkDup = new ShippedOrder[5];

    //Prompt the user for values for each Orders object; do NOT allow duplicate order numbers and force the user to reenter 
    //    the order when a duplicate order number is entered.



            for (int x = 0; x < myShippedOrder.Length; ++x)
            {

                int y = 0;
                myShippedOrder[x] = new ShippedOrder();
                checkDup[y] = new ShippedOrder();

                Console.Write("Please enter the order number for order {0}:  ", x + 1);
                checkDup[y].orderNumber = Convert.ToInt32(Console.ReadLine());             


                for (y = 0; y < checkDup.Length; ++y)
                {
                    if (checkDup[y] != null && myShippedOrder[x].Equals(checkDup[y]))
                    {
                        Console.WriteLine("Sorry, this order number has already been entered, please try again");
                        --x;
                        break;
                    }
                    else myShippedOrder[x].orderNumber = checkDup[y].orderNumber;
                }

                Console.Write("Please enter the customers name for order {0}:  ", x + 1);
                myShippedOrder[x].customerName = Console.ReadLine();

                Console.Write("Please enter the quantity that was ordered for order {0}:  ", x + 1);
                myShippedOrder[x].quantityOrdered = Convert.ToInt32(Console.ReadLine());

                myShippedOrder[x].totalPrice = myShippedOrder[x].quantityOrdered * PRICEEACH + ShippedOrder.SHIPFEE;


        }

any suggestions on how to use the overloaded equal method in the order class to help in finding duplicate orderNumbers if they exist and kick back to the user they entered an invalid order and to try again with that spot in the array?

programmerNOOB
  • 121
  • 3
  • 19
  • 1
    Why not use a Dictionary? Also, any time you overload Equals, you should also overload operator== and GetHashCode(). There are ramifications of overloading Equals, discussed e.g. here: http://stackoverflow.com/questions/9709088/when-should-a-net-class-override-equals-when-should-it-not – Eric J. Apr 19 '12 at 02:17
  • Sorry, I didn't post the whole program but I did overload the others too. I didn't think it is pertinent to this question so I didn't post that code. – programmerNOOB Apr 19 '12 at 02:37

1 Answers1

1

What's happening here is that you need to continue when the user enters something wrong.

Try this:

public static void Main()

{
//Create an array of five ShippedOrder objects. 
    ShippedOrder[] myShippedOrder = new ShippedOrder[5];
    ShippedOrder[] checkDup = new ShippedOrder[5];

//Prompt the user for values for each Orders object; do NOT allow duplicate order numbers and force the user to reenter 
//    the order when a duplicate order number is entered.


        bool wronginput = false;
        for (int x = 0; x < myShippedOrder.Length; ++x)
        {

            int y = 0;
            myShippedOrder[x] = new ShippedOrder();
            checkDup[y] = new ShippedOrder();

            Console.Write("Please enter the order number for order {0}:  ", x + 1);
            checkDup[y].orderNumber = Convert.ToInt32(Console.ReadLine());             


            for (y = 0; y < checkDup.Length; ++y)
            {
                if (checkDup[y] != null && myShippedOrder[x].Equals(checkDup[y]))
                {
                    Console.WriteLine("Sorry, this order number has already been entered, please try again");
                    --x;
                    wronginput = true;
                    break;
                }
                else myShippedOrder[x].orderNumber = checkDup[y].orderNumber;
            }

            if(wronginput)
                continue;

            Console.Write("Please enter the customers name for order {0}:  ", x + 1);
            myShippedOrder[x].customerName = Console.ReadLine();

            Console.Write("Please enter the quantity that was ordered for order {0}:  ", x + 1);
            myShippedOrder[x].quantityOrdered = Convert.ToInt32(Console.ReadLine());

            myShippedOrder[x].totalPrice = myShippedOrder[x].quantityOrdered * PRICEEACH + ShippedOrder.SHIPFEE;


    }

Notice how I added wronginput so that right after the second for loop, it throws back to the beginning of the outer loop.

I think that's what you're looking for right now, even though there are better ways to do what you're doing to check for duplicates.

BeemerGuy
  • 8,139
  • 2
  • 35
  • 46
  • THanks, I guess my other problem is I am getting a "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object." from this line " else myShippedOrder[x].orderNumber = checkDup[y].orderNumber;" – programmerNOOB Apr 19 '12 at 02:23
  • it is line 118 in my code but I am not sure if that what it is in what I posted here. I could post the whole thing if that is helpful? – programmerNOOB Apr 19 '12 at 02:39
  • I added that extra IF check but am stilling getting a runtime error of the instance of an object with line 119 now that I added that bool value in. The line that if the IF passes it will become the next entry in the original array but that is probably a bad idea because that else will run too many times. I need to change that location. – programmerNOOB Apr 19 '12 at 03:00
  • What are you better methods I am all ears, except that I need to incorporate the overrided equals method in the Order class which is a parent of ShippedOrder – programmerNOOB Apr 19 '12 at 03:03