473,403 Members | 2,293 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,403 software developers and data experts.

Need generic enumerator when calling string.split()

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
3 3612
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephanie Stowe | last post by:
Hi. I am trying to understand the weird System.DirectoryServices object model. I have a DirectoryEntry object. I want to enumerate through the PropertyCollection. So I looked at GetEnumerator....
3
by: K.K. | last post by:
I have an instance of type Dictionary<int, string>. What is the type of the enumerator returned by GetEnumerator()? In other words, how would I fill in this blank: Dictionary<int, string>...
2
by: wvtempl | last post by:
From the documentation in MSDN, it looks as though the following should iterate through the collection twice. However, MoveNext in the second iteration returns false: Dim oList As New List(Of...
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
3
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
8
by: Jack | last post by:
Hello, I need to split: 2 1066 1.30 172.90 1065.9 -14.2 3.0 -13.3 0.1 3 1528 1.00 188.10 1527.8 -23.3 3.0 -20.9 0.1 4 2007 0.60 182.60 2006.7 -30.0 2.3 -25.9 0.1 5 2484 1.00 195.20 2483.7...
1
by: DAnDA | last post by:
i got these errors when i want to compile my project : private List<CIF.Business.Northwind.OrdersItem> ConvertListToBusiness (List<CIF.Data.Northwind.OrdersItem> list) {...
2
by: WP | last post by:
Hello, below is my very first python program. I have some questions regarding it and would like comments in general. I won't be able to get my hands on a good python book until tomorrow at the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.