473,382 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Confused about casting.

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[i].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();
Nov 16 '05 #1
3 1537
> 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[].
Nov 16 '05 #2
"MarkT" <Ma***@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
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
Nov 16 '05 #3
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.
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
6
by: Simon | last post by:
Hi all I am writing a small app that uses real numbers all over the place for calculations. But when it comes to displaying values it is better to only display an it, (especially when it...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
2
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3...
3
by: Erwin Moller | last post by:
Hi group, Maybe I should stop working because this seems soo basic. I almost feel ashamed to ask, but here we go. :-/ Consider the following script: <? $name="henk";
3
by: TheMadHatter | last post by:
Yello, Quick Q: If I have a few objects and cast them(?) into an array of a piticular interface, would I have boxed the objects? Thus I wouldnt continuously re-box that type, inturn saving...
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.