Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Cast Problem

Cast Problem

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Reinier van de Wetering
    wrote on last edited by
    #1

    Can someone tell me what is wrong with this code. I've got a couple of custom controls in a groupbox that i need to dispose. I tried the following foreach loop, but i get the error "Specified cast not valid". foreach (WorkersCtrl c in groupBox.Controls) { c.dispose(); } Thanks.

    M R 2 Replies Last reply
    0
    • R Reinier van de Wetering

      Can someone tell me what is wrong with this code. I've got a couple of custom controls in a groupbox that i need to dispose. I tried the following foreach loop, but i get the error "Specified cast not valid". foreach (WorkersCtrl c in groupBox.Controls) { c.dispose(); } Thanks.

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      But that's very simple: at least one of the Controls in groupBox.Controls is not a WorkersCtrl. The Controls collection holds Controls or derived classes, not limited to WorkersCtrl only. If you really only want to call Dispose() then change WorkersCtrl c into Control c and you'll be fine. mav

      R 1 Reply Last reply
      0
      • M mav northwind

        But that's very simple: at least one of the Controls in groupBox.Controls is not a WorkersCtrl. The Controls collection holds Controls or derived classes, not limited to WorkersCtrl only. If you really only want to call Dispose() then change WorkersCtrl c into Control c and you'll be fine. mav

        R Offline
        R Offline
        Reinier van de Wetering
        wrote on last edited by
        #3

        Thanks, it was pretty stupid of me. I've got it working now.

        1 Reply Last reply
        0
        • R Reinier van de Wetering

          Can someone tell me what is wrong with this code. I've got a couple of custom controls in a groupbox that i need to dispose. I tried the following foreach loop, but i get the error "Specified cast not valid". foreach (WorkersCtrl c in groupBox.Controls) { c.dispose(); } Thanks.

          R Offline
          R Offline
          RB Emphasys
          wrote on last edited by
          #4

          You must have a control inside the groupbox that is not of or inherit from WorkersCtl. You could try a couple of different things. You could either throw an if statement in there, to determine if the object is of type WorkersCtrl, and just use the object base class inside your for each decleration. Or...., you could change workersctrl to object, and set a debug statement within the foreach statement, outputing each objects .gettype().tostring() to determine which object is throwing the invalid cast error.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups