none
How to Fix ArgumentOutRangeException in C# WPF RRS feed

  • Question

  •  Hi,

              I Have 10 PhoneNumbers, it take numbers from TextBox. if im add 10 Numbers to listbox it Works fine. But less than 10 Numbers it wont Works, it thrown Exceptions. (For Ex : Now im add only 2 numbers to listbox and click on submit button it shows a exception for Number3 in Code.) 

    Exception : An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in PresentationFramework.dll

    Additional information: Specified argument was out of the range of valid values.

    private SOTA_whitelist_Number RetrieveSOTAConfiguration()
            {
                string Number1;
                string Number2;
                string Number3;
                string Number4;
                string Number5; 
                string Number6;
                string Number7;
                string Number8;
                string Number9;
                string Number10;
                
                Number1 = ListBox_PhoneNumber.Items[0].ToString();
                Number2 = ListBox_PhoneNumber.Items[1].ToString();
                Number3 = ListBox_PhoneNumber.Items[2].ToString();
                Number4 = ListBox_PhoneNumber.Items[3].ToString();
                Number5 = ListBox_PhoneNumber.Items[4].ToString();
                Number6 = ListBox_PhoneNumber.Items[5].ToString();
                Number7 = ListBox_PhoneNumber.Items[6].ToString();
                Number8 = ListBox_PhoneNumber.Items[7].ToString();
                Number9 = ListBox_PhoneNumber.Items[8].ToString();
                Number10 = ListBox_PhoneNumber.Items[9].ToString();
                Console.WriteLine("\n Print Number1 and Number2 {0}, {1}", Number1, Number2);
    
                return new SOTA_whitelist_Number(Number1, Number2, Number3, Number4, Number5, Number6, Number7, Number8, Number9, Number10);
            }

    Monday, January 20, 2020 6:06 AM

Answers

  • Hi,
    try this method:

        private SOTA_whitelist_Number RetrieveSOTAConfiguration()
        {
          string Number1 = string.Empty;
          string Number2 = string.Empty;
          string Number3 = string.Empty;
          string Number4 = string.Empty;
          string Number5 = string.Empty;
          string Number6 = string.Empty;
          string Number7 = string.Empty;
          string Number8 = string.Empty;
          string Number9 = string.Empty;
          string Number10 = string.Empty;
    
          if (ListBox_PhoneNumber.Items.Count > 0) Number1 = ListBox_PhoneNumber.Items[0].ToString();
          if (ListBox_PhoneNumber.Items.Count > 1) Number2 = ListBox_PhoneNumber.Items[1].ToString();
          if (ListBox_PhoneNumber.Items.Count > 2) Number3 = ListBox_PhoneNumber.Items[2].ToString();
          if (ListBox_PhoneNumber.Items.Count > 3) Number4 = ListBox_PhoneNumber.Items[3].ToString();
          if (ListBox_PhoneNumber.Items.Count > 4) Number5 = ListBox_PhoneNumber.Items[4].ToString();
          if (ListBox_PhoneNumber.Items.Count > 5) Number6 = ListBox_PhoneNumber.Items[5].ToString();
          if (ListBox_PhoneNumber.Items.Count > 6) Number7 = ListBox_PhoneNumber.Items[6].ToString();
          if (ListBox_PhoneNumber.Items.Count > 7) Number8 = ListBox_PhoneNumber.Items[7].ToString();
          if (ListBox_PhoneNumber.Items.Count > 8) Number9 = ListBox_PhoneNumber.Items[8].ToString();
          if (ListBox_PhoneNumber.Items.Count > 9) Number10 = ListBox_PhoneNumber.Items[9].ToString();
          Console.WriteLine("\n Print Number1 and Number2 {0}, {1}", Number1, Number2);
    
          return new SOTA_whitelist_Number(Number1, Number2, Number3, Number4, Number5, Number6, Number7, Number8, Number9, Number10);
        }


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Marked as answer by Sahana M Monday, January 20, 2020 7:39 AM
    • Edited by Peter Fleischer Monday, January 20, 2020 9:21 AM delete second semicolon
    Monday, January 20, 2020 7:25 AM

All replies

  • Hi,
    try this method:

        private SOTA_whitelist_Number RetrieveSOTAConfiguration()
        {
          string Number1 = string.Empty;
          string Number2 = string.Empty;
          string Number3 = string.Empty;
          string Number4 = string.Empty;
          string Number5 = string.Empty;
          string Number6 = string.Empty;
          string Number7 = string.Empty;
          string Number8 = string.Empty;
          string Number9 = string.Empty;
          string Number10 = string.Empty;
    
          if (ListBox_PhoneNumber.Items.Count > 0) Number1 = ListBox_PhoneNumber.Items[0].ToString();
          if (ListBox_PhoneNumber.Items.Count > 1) Number2 = ListBox_PhoneNumber.Items[1].ToString();
          if (ListBox_PhoneNumber.Items.Count > 2) Number3 = ListBox_PhoneNumber.Items[2].ToString();
          if (ListBox_PhoneNumber.Items.Count > 3) Number4 = ListBox_PhoneNumber.Items[3].ToString();
          if (ListBox_PhoneNumber.Items.Count > 4) Number5 = ListBox_PhoneNumber.Items[4].ToString();
          if (ListBox_PhoneNumber.Items.Count > 5) Number6 = ListBox_PhoneNumber.Items[5].ToString();
          if (ListBox_PhoneNumber.Items.Count > 6) Number7 = ListBox_PhoneNumber.Items[6].ToString();
          if (ListBox_PhoneNumber.Items.Count > 7) Number8 = ListBox_PhoneNumber.Items[7].ToString();
          if (ListBox_PhoneNumber.Items.Count > 8) Number9 = ListBox_PhoneNumber.Items[8].ToString();
          if (ListBox_PhoneNumber.Items.Count > 9) Number10 = ListBox_PhoneNumber.Items[9].ToString();
          Console.WriteLine("\n Print Number1 and Number2 {0}, {1}", Number1, Number2);
    
          return new SOTA_whitelist_Number(Number1, Number2, Number3, Number4, Number5, Number6, Number7, Number8, Number9, Number10);
        }


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Marked as answer by Sahana M Monday, January 20, 2020 7:39 AM
    • Edited by Peter Fleischer Monday, January 20, 2020 9:21 AM delete second semicolon
    Monday, January 20, 2020 7:25 AM
  • Thank You, Thank You Soo Much ;) ;) It Works Properly.
    Monday, January 20, 2020 7:38 AM
  •  string Number2 = string.Empty; ;
     Can You Explain me, Why 2 SemiColon are Used in ur prgm ?? What is the use 2 SemiColon.
    Monday, January 20, 2020 7:46 AM
  • I want String is Not Empty, It have previously entered value. How to Modify it in ur Code
    Monday, January 20, 2020 9:05 AM
  • Hi,
    2 SemiColon is a result of copy&paste. The second Semicolon is not necessary. I correct my posting.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Monday, January 20, 2020 9:21 AM
  • Hi,
    assign the previous value to variable like in this demo:

        private SOTA_whitelist_Number RetrieveSOTAConfiguration(string previousValue1, ...)
        {
          string Number1 = previousValue1;
    ...


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Monday, January 20, 2020 9:24 AM
  • Now im enter only 2 numbers and click submit button,and close application, after  that i want to add again 4 numbers, but i dont want to delete first 2 numbers, i want to add  4 numbers after that 2 numbers.

    How to add it for above Code.

    Monday, January 20, 2020 9:55 AM