RegEx.MatchCollection.CopyTo method fails!

M

Mortimer Schnurd

Has anyone had any luck getting this CopyTo method to work?

I can iterate through a MatchCollection and move each Match.Value to
the System.Array without a problem. I just can't figure out why the
CopyTo method will not work.

Example:
// Find all the Tables in an html string (page)
MatchCollection mc = Regex.Matches(html, "<table.+</table");
string[] t = new string[mc.Count];
mc.CopyTo(t, 0);

ERROR HERE IS:
An unhandled exception of type 'System.InvalidCastException' occurred
in mscorlib.dll

Additional information: At least one element in the source array could
not be cast down to the destination array type.

-*-*-*-*-*
In my particular case, mc.Count = 4. If I use a for each loop to
iterate through and set each element of the array, there is no problem
whatsoever.

TIA for any information that may help me to understand why the CopyTo
did not work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top