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. casting (headaches)

casting (headaches)

Scheduled Pinned Locked Moved C#
databasehelptutorialquestion
4 Posts 2 Posters 6 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.
  • S Offline
    S Offline
    Senkwe Chanda
    wrote on last edited by
    #1

    where x is the 0 based index of the control previously added. The problem is, that snippet of code as it stands returns (and rightly so) a generic Sytem.Forms.Control object. My problem is that I need to change an aspect of the TilePlaceControl actually contained within the panel...for example, I need to do something like this... myPanel.Controls[x].MyMethod(...) So then, why can I NOT do this? (TilePlaceControl)myPanel.Controls[x].MyMethod(...) The compiler complains saying that MyMethod is not a member of System.Forms.Control. I wouldn't mind so much if I could simply do the following... TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; tpc.MyMethod(...) myPanel.Controls[x] = tpc; But I can't because Controls[x] is a READ ONLY indexer. Please guys give me a way around this, the other alternative is just way too ugly to even contemplate Regards, Senkwe :confused: :confused: :confused: Just another wannabe code junky

    J 1 Reply Last reply
    0
    • S Senkwe Chanda

      where x is the 0 based index of the control previously added. The problem is, that snippet of code as it stands returns (and rightly so) a generic Sytem.Forms.Control object. My problem is that I need to change an aspect of the TilePlaceControl actually contained within the panel...for example, I need to do something like this... myPanel.Controls[x].MyMethod(...) So then, why can I NOT do this? (TilePlaceControl)myPanel.Controls[x].MyMethod(...) The compiler complains saying that MyMethod is not a member of System.Forms.Control. I wouldn't mind so much if I could simply do the following... TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; tpc.MyMethod(...) myPanel.Controls[x] = tpc; But I can't because Controls[x] is a READ ONLY indexer. Please guys give me a way around this, the other alternative is just way too ugly to even contemplate Regards, Senkwe :confused: :confused: :confused: Just another wannabe code junky

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      Senkwe Chanda wrote: (TilePlaceControl)myPanel.Controls[x].MyMethod(...) Almost have it! ((TilePlaceControl)myPanel.Controls[x]).MyMethod(...) That will give you what you want :) [Note the extra set of parenthesis] Senkwe Chanda wrote:

      = tpc;
      So close, yet again :) You shouldn't have to assign back into the collection unless you actually want to change the instance in the collection in which case you should do a Controls.Remove and Controls.Add. The code below should work, and give you the results you expect, as will the code at the top. TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; tpc.MyMethod(...) James Sonork ID: 100.11138 - Hasaki

      S 1 Reply Last reply
      0
      • J James T Johnson

        Senkwe Chanda wrote: (TilePlaceControl)myPanel.Controls[x].MyMethod(...) Almost have it! ((TilePlaceControl)myPanel.Controls[x]).MyMethod(...) That will give you what you want :) [Note the extra set of parenthesis] Senkwe Chanda wrote:

        = tpc;
        So close, yet again :) You shouldn't have to assign back into the collection unless you actually want to change the instance in the collection in which case you should do a Controls.Remove and Controls.Add. The code below should work, and give you the results you expect, as will the code at the top. TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; tpc.MyMethod(...) James Sonork ID: 100.11138 - Hasaki

        S Offline
        S Offline
        Senkwe Chanda
        wrote on last edited by
        #3

        Thanks alot James, your first code snippet works great and is even clear now that I see it in black and white. As for your last piece of code TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; tpc.MyMethod(...) I thought of that but then thought that TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; creates a seperate instance of the object and hence, tpc.MyMethod(...) would still not be called on the instance contained by the panel. But I'll explore that again. Thanks alot!! Regards Senkwe Just another wannabe code junky

        J 1 Reply Last reply
        0
        • S Senkwe Chanda

          Thanks alot James, your first code snippet works great and is even clear now that I see it in black and white. As for your last piece of code TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; tpc.MyMethod(...) I thought of that but then thought that TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; creates a seperate instance of the object and hence, tpc.MyMethod(...) would still not be called on the instance contained by the panel. But I'll explore that again. Thanks alot!! Regards Senkwe Just another wannabe code junky

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          Senkwe Chanda wrote: I thought of that but then thought that TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x]; creates a seperate instance of the object and hence, tpc.MyMethod(...) would still not be called on the instance contained by the panel. Nope, tpc is only a reference to the object. A reference is similar to a pointer in C/C++ and ByRef in VB; anything you do to the reference (except assignment) will carry through to the actual instance. Assignment on a reference only makes the reference refer/point to something else, leaving the original intact. The only way to create a new instance is via the new keyword or through reflection's Activator.CreateInstance, neither of which you show here :) James Sonork ID: 100.11138 - Hasaki

          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