0

Thanks for your feedback. I'm still getting an out of range exception. I'm debugging but can't understand why I'm getting the exception. If an int is higher than a certain other int it should work. Does it have anything to do with the length.Count?

UPDATED Code

                    do
                {
                    Console.Write("Input the element that you want to exclude from the list: ");
                    result = int.TryParse(Console.ReadLine(), out delSelection);

                    tempInputDelInt = (int)(tempList[delSelection]);
                    tempInputDelStr = Convert.ToString(tempInputDelInt);

                    if (result == true && tempInputDelInt >= 0 && tempInputDelInt < tempList.Count)
                    {
                        tempList.RemoveAt(delSelection);
                        Console.WriteLine("\nYou've deleted the temperature " + tempInputDelStr + " from index " + delSelection);
                        success = false;
                        Console.ReadKey(true);
                        Console.Clear();
                    }

                    else if (result == true && tempInputDelInt >= tempList.Count || tempInputDelInt < 0)
                    {
                        Console.WriteLine("You've input a number that's outside the list. Input a digit between 0 and " + (tempList.Count - 1) + ".\n");
                        success = true;
                        Console.ReadKey(true);
                    }

                    else if (result == false)
                    {
                        Console.WriteLine("\nYou didn't input a digit, try again!\n");
                        success = true;
                        Console.ReadKey(true);
                    }
                } while (success);
                success = false;

I'm having some problems with the validation of the first else if statement. I want to catch the cases when a value outside of the array is input, but I don't manage to.

I've done a practically identical program with array.Length with success.

Is there any difference between array.List and list.Count? Is that's the problem?

Thanks in advance for your help!

                    do
                {
                    Console.Write("Input the element that you want to exclude from the list: ");
                    result = int.TryParse(Console.ReadLine(), out delSelection);


                    tempInputDel = Convert.ToString(tempList[delSelection]);

                    if (result == true && delSelection >= 0 && delSelection < tempList.Count)
                    {
                        tempList.RemoveAt(delSelection);
                        Console.WriteLine("\nYou've deleted the temperature " + tempInputDel + " from index " + delSelection);
                        Console.ReadKey(true);
                        Console.Clear();
                    }

                    else if (result == true && delSelection >= tempList.Count || delSelection < 0)
                    {
                        Console.WriteLine("You've input a number that's outside the list. Input a digit between 0 and " + (tempList.Count - 1) + ".\n");

                    }

                    else if (result == false)
                    {
                        Console.WriteLine("\nYou didn't input a digit, try again!");
                        Console.ReadKey(true);
                    }
                } while (result == false);
Max
  • 488
  • 8
  • 19

2 Answers2

1

Change this

tempInputDelInt = (int)(tempList[delSelection]);

to this

tempInputDelInt = (int)delSelection;

Whatever number the user enters, you're using that as the index to determine which item to delete. So it's not a value you get from tempList.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
0

I am assuming that your out-of-range exception happens here:

tempInputDel = Convert.ToString(tempList[delSelection]);

You cannot do this before checking, if delSelection is inside range of tempList. Move it inside first if block.

General advice:

You can use a debugger to go from statement to statement step-by-step to find the exact source code position where (e.g.) an exception is thrown.

RhinoDevel
  • 712
  • 1
  • 12
  • 25