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

Need generic enumerator when calling string.split()

P: n/a
I'm calling string.Split() producing output string[]. I need direct
access to its enumerator, but would greatly prefer an enumerator
strings and not object types (as my parsing is unsafe casting from
object to string frequently). Basically generics and not its non-
generic counterpart.

string str1 = "abc: value1 def: value2 ghi: value3";
char[] delimiterChars = { '\t' };
string[] tokens = str1.Split(delimiterChars);

// Bad cast of course but essentially what i need
System.Collections.Generic.IEnumerator<stringtoken sEnum =
(System.Collections.Generic.IEnumerator<string>)to kens.GetEnumerator();

// Cannot use foreach as i need direct access to the enumerator within
the loop
while (tokensEnum.MoveNext())
{
string fieldValue = tokensEnum.Current; // without cast
// use tokensEnum directly below when parsing (checking for
iterator invalidation)....
}

the bad cast produces error as expected:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'SZArrayEnumerator' to type
'System.Collections.Generic.IEnumerator`1[System.String]'."

Oct 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dave <dj*********@gmail.comwrote:
I'm calling string.Split() producing output string[]. I need direct
access to its enumerator, but would greatly prefer an enumerator
strings and not object types (as my parsing is unsafe casting from
object to string frequently). Basically generics and not its non-
generic counterpart.

string str1 = "abc: value1 def: value2 ghi: value3";
char[] delimiterChars = { '\t' };
string[] tokens = str1.Split(delimiterChars);

// Bad cast of course but essentially what i need
System.Collections.Generic.IEnumerator<stringtoken sEnum =
(System.Collections.Generic.IEnumerator<string>)to kens.GetEnumerator();

// Cannot use foreach as i need direct access to the enumerator within
the loop
while (tokensEnum.MoveNext())
{
string fieldValue = tokensEnum.Current; // without cast
// use tokensEnum directly below when parsing (checking for
iterator invalidation)....
}

the bad cast produces error as expected:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'SZArrayEnumerator' to type
'System.Collections.Generic.IEnumerator`1[System.String]'."
How about this:

IEnumerable<stringtokens = str.Split(delimiterChars);

IEnumerator<stringtokensEnum = tokens.GetEnumerator();

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 2 '07 #2

P: n/a
On Oct 1, 4:56 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Dave <djohanns...@gmail.comwrote:
I'm calling string.Split() producing output string[]. I need direct
access to its enumerator, but would greatly prefer an enumerator
strings and not object types (as my parsing is unsafe casting from
object to string frequently). Basically generics and not its non-
generic counterpart.
string str1 = "abc: value1 def: value2 ghi: value3";
char[] delimiterChars = { '\t' };
string[] tokens = str1.Split(delimiterChars);
// Bad cast of course but essentially what i need
System.Collections.Generic.IEnumerator<stringtoken sEnum =
(System.Collections.Generic.IEnumerator<string>)to kens.GetEnumerator();
// Cannot use foreach as i need direct access to the enumerator within
the loop
while (tokensEnum.MoveNext())
{
string fieldValue = tokensEnum.Current; // without cast
// use tokensEnum directly below when parsing (checking for
iterator invalidation)....
}
the bad cast produces error as expected:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'SZArrayEnumerator' to type
'System.Collections.Generic.IEnumerator`1[System.String]'."

How about this:

IEnumerable<stringtokens = str.Split(delimiterChars);

IEnumerator<stringtokensEnum = tokens.GetEnumerator();

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too- Hide quoted text -

- Show quoted text -
Perhaps close but no, this does not seem to work.

tokens is not broken up into buckets as it is when receiving
string.Split() into string[] array.
I.e. The tokens enumerator has same value as original string "abc:
value1 def: value2 ghi: value3" with only 1 dimension (rather
than 3).

Oct 2 '07 #3

P: n/a
On Oct 1, 6:07 pm, Dave <djohanns...@gmail.comwrote:
On Oct 1, 4:56 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:


Dave <djohanns...@gmail.comwrote:
I'm calling string.Split() producing output string[]. I need direct
access to its enumerator, but would greatly prefer an enumerator
strings and not object types (as my parsing is unsafe casting from
object to string frequently). Basically generics and not its non-
generic counterpart.
string str1 = "abc: value1 def: value2 ghi: value3";
char[] delimiterChars = { '\t' };
string[] tokens = str1.Split(delimiterChars);
// Bad cast of course but essentially what i need
System.Collections.Generic.IEnumerator<stringtoken sEnum =
(System.Collections.Generic.IEnumerator<string>)to kens.GetEnumerator();
// Cannot use foreach as i need direct access to the enumerator within
the loop
while (tokensEnum.MoveNext())
{
string fieldValue = tokensEnum.Current; // without cast
// use tokensEnum directly below when parsing (checking for
iterator invalidation)....
}
the bad cast produces error as expected:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'SZArrayEnumerator' to type
'System.Collections.Generic.IEnumerator`1[System.String]'."
How about this:
IEnumerable<stringtokens = str.Split(delimiterChars);
IEnumerator<stringtokensEnum = tokens.GetEnumerator();
--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too- Hide quoted text -
- Show quoted text -

Perhaps close but no, this does not seem to work.

tokens is not broken up into buckets as it is when receiving
string.Split() into string[] array.
I.e. The tokens enumerator has same value as original string "abc:
value1 def: value2 ghi: value3" with only 1 dimension (rather
than 3).- Hide quoted text -

- Show quoted text -
oops oops, your idea works fine!

i was having problems with my iterator so case closed. thanks for
your help.

Oct 2 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.