473,287 Members | 1,588 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,287 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 3605
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.