NI TestStand

Showing results for 
Search instead for 
Did you mean: 

regular expression match collection

Go to solution


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)


0 Kudos
Message 1 of 4

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.

Daniel E.
TestStand Staff Product Support Engineer
National Instruments
Message 2 of 4
Accepted by topic author NikolajEgeskovOstergaard

Hi Daniel-E


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...


br Nikolaj

0 Kudos
Message 3 of 4
Accepted by topic author NikolajEgeskovOstergaard



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.

Daniel E.
TestStand Staff Product Support Engineer
National Instruments
0 Kudos
Message 4 of 4