Tech Off Thread

12 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Specified cast is not valid in asp.net/c# foreach loop

Back to Forum: Tech Off
  • User profile image
    ahockley

    Bashing my head against this and seeking ideas.  I have an ASP.NET page which contains a bunch of checkboxes in a panel.  They are standalone checkboxes, not a checkboxlist and need to be that way so that they can be formatted on the page.

    Upon submitting the form, I need to loop through each checkbox and see if it is checked, and take action.  Here's my code:

    private void butSubmit_Click(object sender, System.EventArgs e)
    {
     
    foreach (CheckBox ck in pnlCheckboxes.Controls)
      {

          if(ck.Checked == true)
          {
              ... do stuff

    The code compiles and at runtime errors at the foreach statement with the error "Specified cast is not valid"

    Any ideas?  I've used similar code to loop through the checkboxes in a checkboxlist, but need to do it with standalone controls.

    Aaron

  • User profile image
    object88

    Perhaps there is some non-CheckBox control within pnlCheckboxes?  Can you step through the code and manually validate that each item in pnlCheckboxes.Controls is a CheckBox?  If not, the foreach will throw the error.

  • User profile image
    Sk4rlath

    You could always just change the foreach variable to something more generic and use as to check the type:

    foreach (object o in pnlCheckboxes.Controls)
    {
       CheckBox chk = o as CheckBox;
    
       if (chk == null)
          continue;
    
       if (chk.Checked)
       {
          // your code
       }
    }

  • User profile image
    object88

    Does using "as" not throw a cast exception if "o" is of the wrong type?  Neat, I didn't know that... I always did soemthing like

    if (o is CheckBox)
    {
       CheckBox chk = (CheckBox)chk;
       // ...
    }

  • User profile image
    ahockley

    Thanks for the replies, that info helped... by checking the object type I only attempt to deal with CheckBoxes.

  • User profile image
    Jorgie

    Wow, that was quick. I learn something new everyday!

  • User profile image
    littleguru

    In ASP.NET you get for each string you enter in the page also a control: its called LiteralControl. Check it out in the documentation. I guess you got some of those.

    The ideas that has been supposted here are not bad...

    You could eventually also use a for-loop. The foreach loop with a cast to object is not that nice...

    Cheers
    Christian

  • User profile image
    Kaelan

    This is one thing that really bugs me about C#. In VB6 I could do this:
    Dim blah as SomeObject
    For Each blah in SomeCollection
     blah.foo
    Next blah
    And it would iterate through all the SomeObjects in SomeCollection, and ignore everything else.
    In C# it errors out. Sad

  • User profile image
    Sk4rlath

    as tries to cast the object to the specified type and on failure it returns null.

    Extra handy.

  • User profile image
    object88

    Although with just plane "as", you still have to check for null.  I'd like so see something like

    with (CheckBox chk = o as CheckBox)
    {
       // body
    }

    where body is only executed if o is a CheckBox.  This could be expanded out to...

    with (CheckBox chk = o as CheckBox)
    {
       // body
    }
    else with (ToolBarButton tbb = o as ToolBarButton)
    {
       // other body
    }
    else
    {
       // another body
    }

    That would be useful to me, and quite compact.  But I think "with" is a VB keyword, isn't it?

  • User profile image
    Sampy

    VB does have a with keyword:

    With New OpenFileDialog()
       .Filter = "Text file|*.txt"
       If .ShowDialog() = DialogResult.Ok
          ' do something with the file
       End If
    End With

    Inside the With you can use . to refer to the With'ed variable. I think that I read somewhere that you get a small perf boost if you do this in a place where you set a lot of properties.

  • User profile image
    EJohnson

    This works!!!:

    foreach (ListItem checkbox in myCheckBoxList.Items)
    {
       checkbox.Selected = true;
    }


    More info can be found here:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtsksettingselectioninlistbox.asp



    Hope that helps.


    (Sorry, this is not specific to your problem)

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.