In the attached "regex test.seq" i'm trying to copy a match collection to an array, to be able to "for each" over all mathes.
Use Existing Object(System.Text.RegularExpressions.MatchCollection).CopyTo(System.Array, Int32)
What am I doing wrong ?
An exception occurred inside the call to .NET member 'CopyTo':
System.ArgumentNullException: Value cannot be null.
Parameter name: dest
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.ArrayList.CopyTo(Array array, Int32 arrayIndex)
at System.Text.RegularExpressions.MatchCollection.CopyTo(Array array, Int32 arrayIndex)
Solved! Go to Solution.
I believe you are getting this error because the CopyTo() method expects the target array to be an initialized Array object, rather than an empty Object Reference. I was able to successfully do this with the following process:
1) One of your earlier steps calls MatchCollection.Item.ToString(). Part of this call returns an Item object, which you need to save so that you can get the type of it for creating the array. I saved the Item object in an Object Ref variable named MatchObject.
2) I added a new .NET adapter step which calls GetType() on the MatchObject variable and saves this value (a System.Type) to another variable named MatchArrayType. You need this type to create the array.
3) I added another .NET step to create the array using the System.Array.CreateInstance() method in the mscorlib library. This method requires a System.Type for the array type, as well as a size. The type comes from the MatchArrayType variable, and the size comes from another variable that you set previously (Locals.groupCount, I believe). I stored the new array in your Locals.arrayObj variable.
This process correctly initializes the array object so that the CopyTo function works correctly.
I'm attaching a file (saved in TestStand 2012) with these changes. Let me know if you have any trouble with the example.
Thanks for your support.
I see your point, but i got stuck at "For Each" over the "array obj". Is this possible ?
Instead i found another way.
The aim is to read out all the matched groups.
In the attached .seq I used a normal For-loop from 0 to the group count, and then just readout each one...
You are correct. You cannot directly use a For-Each loop in TestStand over a .NET object reference. Your alternate approach seems like a reasonable solution to this.
The other approach you could take would be to create a .NET assembly to implement this functionality, and call the assembly from TestStand as needed. Your assembly could return a standard TestStand array of strings, which you could iterate over in a For-Each loop within TestStand.