Problem with foreach loop

Discussion in 'ASP .Net' started by =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004.

  1. 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.ToString();
    }

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

    Thanks, Justin.
    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?SnVzdGlu?=

    Thomas Guest

    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 ...


    "Justin" <> wrote in message
    news:...
    > 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.ToString();
    > }
    >
    > I get an "invalid cast" error! and I can't seem to find the solution.
    >
    > Thanks, Justin.
    Thomas, Oct 22, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?SnVzdGlu?=

    Hans Kesting Guest

    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.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.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
    Hans Kesting, Oct 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    375
    Luc Kumps
    Aug 22, 2003
  3. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    739
    Scott Allen
    Feb 12, 2005
  4. Vinod. K
    Replies:
    4
    Views:
    93
    Anno Siegel
    Aug 25, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page