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