Confused about casting.

B

Barry Mossman

Hi,
I get the feeling that I am missing something with regards to casting.

The CopyTo method allows me to copy the contents of a collection into an
array. My collection is a MatchCollection produced by Regex. The
Collection's entries are all valid strings. If I CopyTo to a string array
it compiles ok, but fails at run time with an InvalidCastException. It
works if I CopyTo an object array. ... does this mean that the
MatchCollection's CopyTo should only be used with arrays of objects ?

If I use the array of objects how can I use IndexOf to find the element
that contains a specific string ? The command I was using is, where I was
looking for the word "two":
int ix = Array.IndexOf(WordArray,(object)"two"); // fails

It all works if I don't use CopyTo, but instead step through the
collection using a for loop, and doing a cast to string as follows:
string[] MyStringArrayxx = new string[mc.Count];
for (int i2 = 0; i2 < mc.Count; i2++) {
MyStringArrayxx[i2] = mc[i2].ToString();
}
ix = Array.IndexOf(MyStringArrayxx,"two");
Console.WriteLine("String array -> {0}",ix);

I get the feeling that I missing something. ie. is there a way to
overcome the cast exception with CopyTo, or is there a way to use IndexOf
with an array of objects ?

Thanks
Barry Mossman

Source in context follows:
Regex regex = new Regex(
@"[^\s]\w*",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);

string stx = "One or two two words.";
MatchCollection mc = regex.Matches(stx);
Console.WriteLine("captures = {0}",mc.Count);
for (int i = 0; i < mc.Count; i++) {
Console.WriteLine("match = {0}", mc.Value);
}

try {
Console.WriteLine(mc.GetType());
Console.WriteLine(mc[0].GetType());

string[] StringArray = new string[mc.Count];
//mc.CopyTo(StringArray,0); // causes InvalidCastException


// don't know how to use IndexOf with objects
object[] WordArray = new object[mc.Count];
mc.CopyTo(WordArray,0);
int ix = Array.IndexOf(WordArray,(object)"two"); // fails
Console.WriteLine("Object array -> {0}",ix);


// all ok if I step through and convert all entries
// to strings
string[] MyStringArrayxx = new string[mc.Count];
for (int i2 = 0; i2 < mc.Count; i2++) {
MyStringArrayxx[i2] = mc[i2].ToString();
}
ix = Array.IndexOf(MyStringArrayxx,"two");
Console.WriteLine("String array -> {0}",ix);

}
catch (Exception ex) {
Console.WriteLine(ex);
}

Console.ReadLine();
 
G

Guest

The CopyTo method allows me to copy the contents of a collection into an
array. My collection is a MatchCollection produced by Regex. The
Collection's entries are all valid strings.

The collection is not a collection of strings, it is a collection of Match
objects.

Try passing a Match[] to CopyTo instead of a string[].
 
B

Barry Mossman

MarkT said:
The CopyTo method allows me to copy the contents of a collection into an
array. My collection is a MatchCollection produced by Regex. The
Collection's entries are all valid strings.

The collection is not a collection of strings, it is a collection of Match
objects.

Try passing a Match[] to CopyTo instead of a string[].

Thanks for the response Mark.

I did think of that, but I was not much further ahead than I was with
the MatchCollection.

I was wanting to use IndexOf to to jump to one of the matches.
int ix = Array.IndexOf(WordArray,(<<???>>)"two");

Match hasn't got a public constructor.

Is there any way to use IndexOf when you have an array of Match, or
object for that matter ?
or would i need to scrap Arrays, and use a Hashtable.

I had the feeling that i should be able to do:
int ix = Array.IndexOf(WordArray,(object)"two");
somehow, but couldn't see how to code it.

Barry mossman
 
G

Guest

Could you just foreach through the MatchCollection and pull out the Value of
each Match and load them into a String[]? That's not as convenient as CopyTo,
but it gets you the data in the form you want. IndexOf should then work
against the String[].
I was wanting to use IndexOf to to jump to one of the matches.
int ix = Array.IndexOf(WordArray,(<<???>>)"two");

I think the problem is that you are trying to compare the String object you
pass in against the Match objects in the array - so I would bet you always
get back "not found".
Is there any way to use IndexOf when you have an array of Match, or
object for that matter ?

Not that I can see, but I've never used Match before so I can't say for
sure. Perhaps someone who has used this stuff a lot will jump in with a good
idea.

Remember, you are dealing with an array of references, not an array of
actual instances. The objects pointed to by the references in the array are
and will always be Match. The type of the array you pass to CopyTo determines
the type of the references in your array, not the type of the objects they
are pointing at.
I had the feeling that i should be able to do:
int ix = Array.IndexOf(WordArray,(object)"two");
somehow, but couldn't see how to code it.

The cast only converts the String reference to an Object reference - it does
not change the actual "thing". Casting with reference types is like this. The
cast just gives you a different type of reference, it does not change the
actual type of the object.
 

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