Page 1 of 1

ArgumentOutOfRangeException

Posted: Sat Jun 19, 2010 5:22 pm
by Gunner1980
Can anyone help?

I have the following code which causes the following exception. I am using this code all over the place and it happens everywhere I have it but not every time.

Here is the error

Unexpected exception occured: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at TacViewLib.LinkSpecific.Surveillance.Air.CreateTrack.CreateTrackRegression.PassiveActiveval()
at TacViewLib.LinkSpecific.Surveillance.Air.CreateTrack.CreateTrackRegression.Start()
at Link_16_Regression.program.J3_2Test(Int32& error)


Here is the code

Code: Select all

public static void Strengthval()
		{
			repo.FormMainCreate.ListItemStrength.Click();
		    Keyboard.Press(Keys.Tab);
		    Keyboard.Press("{LMenu down}{Down}{LMenu up}");
			Ranorex.List list2 = "//list[@processname='RealView']";
            int AScount = list2.Items.Count;
            Random ASvalue = new Random();
            AScount = AScount - 1;
			int ASnum = ASvalue.Next(1, AScount);
       		repo.ListRealView.Self.Items[ASnum].Select();
       		repo.ListRealView.Self.Items[ASnum].Click();
			Accessible accElement = new Accessible(repo.FormMainCreate.ListItemStrength);  
   			string StrengthValue = accElement.Value;
   			ValidateTrack.ValidateTrackRegression.Strength = StrengthValue;
   			Report.Info("Strength Value Entered = " + StrengthValue);
		}
The code is supposed to open a drop down menu with the {LMenu down}{Down}{LMenu up} key sequence and then query how many items are in the drop down. It is then supposed to pick a random number between 1and the range of how many items are in the drop down and click on it.

Re: ArgumentOutOfRangeException

Posted: Mon Jun 21, 2010 2:36 pm
by Support Team
The only problem I see in the code that could cause the exception is that you use two different instances of the list. In your code you first use the list2 instance to get the number of items, but then you use another instance from the repository to access the items. I don't know the repository, so I can only guess that the two instances have the same path. Anyway, I would get the instance from the repository, store it in the list2 variable, and from then on use the variable to access the list.
Ranorex.List list2 = repo.ListRealView.Self;
...
list2.Items[ASnum].Select();
...
Two other things that caught my eye:
  • List indexes in .NET are zero-based, i.e. maybe you should let your random number go from zero to AScount - 1.
  • If there are zero items in the list, the call to Random.Next will throw an exception. I don't know if that case can happen in your application, but you might want to consider that.
Regards,
Alex
Ranorex Team

Re: ArgumentOutOfRangeException

Posted: Mon Jun 21, 2010 3:23 pm
by Gunner1980
Thanks, I will try those things out and let you know. The reason I am starting at 1 is because there are valid 0 values in the list that I do not want selected.