Click here to Skip to main content
14,732,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi frnds i have an checked list box the multiple vales am selecting in checkedlistbox should bound in an array and i gt through string i used an code bt i got an error as

Index was outside the bounds of the array.


Here is my code pls help....

foreach (string s in checkedListBox1.CheckedItems)
           {
               string[] arr = new string[] { s.ToString() };
               string lk=arr[0].ToString();
               string lk1 = arr[1].ToString();
           }


pls help me thank u....
Posted
Comments
Jameel VM 16-Jul-13 0:47am
   
in which line the error got?
usha C 16-Jul-13 0:49am
   
string lk1 = arr[1].ToString(); here i got error
Jameel VM 16-Jul-13 0:55am
   
I have update my answer.Please put that code

Please change your code like below
var lst = new List<string>();
          foreach (string s in checkedListBox1.CheckedItems)
          {
              lst.Add(s);

          }
          string lk = lst[0];
          string lk1 = lst[1];



Hope this helps
   
v2
Comments
Jameel VM 16-Jul-13 0:58am
   
I have modified my answer. It's work fine. the error is because when you are try to take the value of array[1],there is only one item in that string array in the postio of array[0]. Please take the value after the loop execute
usha C 16-Jul-13 0:58am
   
ya am getting nw thnk u...
usha C 16-Jul-13 0:58am
   
Thank you sir....
This error will be shown when you try to access a array location which is not available at runtime. This can be due to the array not being given the appropriate values or it just receives a value which is not expected.

Before accessing any index of the array do a check with the following property:

array.Length

Example:

string[] array = new string[] { yourInput.ToString() };
string text = string.Empty;

if(array.Length > 0)
    text = array[0];
   

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