473,396 Members | 1,970 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,396 software developers and data experts.

Problem with foreach loop

I don't use the foreach statement often so this problem has cought me off
guard. Here is the code:

foreach (int i in EventDates.Items)
{
DateList += EventDates.Items[i].ToString();
}

I get an "invalid cast" error! and I can't seem to find the solution.

Thanks, Justin.
Nov 18 '05 #1
2 1027
Justin wrote:
I don't use the foreach statement often so this problem has cought me
off guard. Here is the code:

foreach (int i in EventDates.Items)
{
DateList += EventDates.Items[i].ToString();
}

I get an "invalid cast" error! and I can't seem to find the solution.

Thanks, Justin.


You don't specify the type of EventDates or DateList, but there is (at least)
one thing wrong:
the type in the foreach ("int i" in your case) should be a member of the collection,
not an index!

either change it to:
for (int i = 0; i<EventDates.Items.Count; i++)
DateList += EventDates.Items[i].ToString();

or to:
foreach (DateListItem dli in DateList.Items)
DateList += dli.ToString();

(change the types to the appropriate ones, I had to guess)

The "for" version might be a bit faster than "foreach"!
Hans Kesting
Nov 18 '05 #2
The cast error occures because the item in EventDates is probably not an int
....

You could write:

foreach( object o in EventDates.Items ) {
if ( o is int ) {
int myInt = o;
DateList += o.ToString();
}
}

PS: Remember that (in your code) that the int i is the item, not an index to
be used like EventDates.Items[i] ...
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:AB**********************************@microsof t.com...
I don't use the foreach statement often so this problem has cought me off
guard. Here is the code:

foreach (int i in EventDates.Items)
{
DateList += EventDates.Items[i].ToString();
}

I get an "invalid cast" error! and I can't seem to find the solution.

Thanks, Justin.

Nov 18 '05 #3

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

Similar topics

3
by: Paul Porcelli | last post by:
I have the following code(excerpt) which grabs some lines from a syslog file and adds any found in the range to an array. @lines=();@vvlines=(); $t = new Net::Telnet (Timeout => 30, Prompt =>...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
8
by: Dave Hagerich | last post by:
I'm using a DataGrid with a DataSet and I'm trying to filter the data being displayed, using the following code as a test: DataView theView = new DataView(theDataSet.Tables); theView.RowFilter =...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
9
tlhintoq
by: tlhintoq | last post by:
Situation: A Windows Form with a single User Control containing 4 custom controls. This lets me drop the User Control on its own form to make it a dialog, or make it part of a larger configuration...
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
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.