Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


What I have tried:

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);
        }


[UPDATE] :
I tried with For loop, But it not works, same exception will be thrown.

string[] Number = new string[20];
for (i = 1; i <= ListBox1.Items.Count; i++)
{
     Number[i] = ListBox1.Items[i].ToString();
}
Posted
Updated 19-Jan-20 21:57pm
v2

Arrays are 0-based indexed; this means that they are indexed from zero to (number of elements - 1).
Moreover, in your code you should not hard-code the array size, but rather construct the array from the number of elements in the list.
C#
int count = ListBox1.Items.Count;
string[] Number = new string[count];
for (i = 0; i < count; i++)
{
     Number[i] = ListBox1.Items[i].ToString();
}

This should at least get rid of the array index exception.

As advised in solution 1, you should get a better understanding of arrays:
c# working with arrays[^] will present you with a list of learning sources.
 
Share this answer
 
Quote:
How fix argumentoutofrangeexception ?

Your problem is that your code is designed to work only with 10 phone numbers, no more, no less.
You need to redesign your code to work with a variable number of phone numbers.

[Update]
Quote:
How can i do code for less than 10 Numbers ?? Please give some ideas

you need to learn programming with loops and arrays.
 
Share this answer
 
v2
Comments
Member 14672509 20-Jan-20 1:21am    
How can i do code for less than 10 Numbers ?? Please give some ideas
Patrice T 20-Jan-20 1:36am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 14672509 20-Jan-20 1:44am    
I have Updated My Code.
Dave Kreskowiak 20-Jan-20 10:57am    
If your loop, your using the Count of items in the array. This will Count value will be one higher than that higher index value, so if you have a Count of 20, the index values are from 0 to 19, inclusive. There is no index 20.

Now loop at the condition in your for loop. You're doing i <= ListBox1.Items.Count. CHange the "less than or equal to" to just "less than".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900