By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,588 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,588 IT Pros & Developers. It's quick & easy.

RegEx.MatchCollection.CopyTo method fails!

P: n/a
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.

--
John Wood a.k.a. Mortimer Schnurd
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

The array t should be an array of Match objects, not strings.

Brian Davis
http://www.knowdotnet.com

"Mortimer Schnurd" <fu**********@hotsmail.com> wrote in message
news:s3********************************@4ax.com...
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.

--
John Wood a.k.a. Mortimer Schnurd

Nov 16 '05 #2

P: n/a
On Wed, 16 Jun 2004 16:31:49 -0400, in msg
<#c**************@TK2MSFTNGP09.phx.gbl>, "Brian Davis" <@> wrote:

The array t should be an array of Match objects, not strings.

Brian Davis
http://www.knowdotnet.com

That was it!

Thanks for the help.

MatchCollection mc = Regex.Matches(html, "<table.+</table");
Match[] t = new Match[mc.Count];
mc.CopyTo(t,0);

--
JW
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.