Class library usage, coding and language questions.
User avatar
Posts: 89
Joined: Mon Apr 05, 2010 8:44 pm
Location: Austin, Texas


Post by Gunner1980 » Sat Jun 19, 2010 5:22 pm

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()
		    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);
			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.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12169
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA

Re: ArgumentOutOfRangeException

Post by Support Team » Mon Jun 21, 2010 2:36 pm

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;
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.
Ranorex Team

User avatar
Posts: 89
Joined: Mon Apr 05, 2010 8:44 pm
Location: Austin, Texas

Re: ArgumentOutOfRangeException

Post by Gunner1980 » Mon Jun 21, 2010 3:23 pm

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.