-
Jun 28th, 2008, 07:41 AM
#1
Thread Starter
Lively Member
[3.0/LINQ] Foreach loop problem?
Hello!
I am trying to loop through all the controls in my form and for each myCustomControl I wish to check the tag for equality with a string, but since I have other controls in my form as well I get the error similar to:
"It didn't work to convert an object of type System.Windows.Forms.ToolStrip into the type MyProject.MyCustomControl."
Anyone know how I can fix this? I almost bet it's something real easy... ^^
Code:
foreach (MyCustomControl myControl in this.Controls)
{
if (myControl.Tag.ToString() == myString)
{
myMethod();
}
}
Cheers,
//Zolomon
-
Jun 28th, 2008, 08:28 AM
#2
Hyperactive Member
Re: [3.0/LINQ] Foreach loop problem?
Microsoft's solution to problems like this is as follows:
Code:
foreach(Control ctl in this.Controls) {
try {
MyCustomControl mycc = (MyCustomControl)ctl);
// do your if statement here since you know it's of type MyCustomControl
} catch { /* Silently handle Conversion errors */ }
}
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jun 28th, 2008, 11:09 AM
#3
Thread Starter
Lively Member
Re: [3.0/LINQ] Foreach loop problem?
Ah, thanks!
Working wonders. (:
-
Jun 28th, 2008, 11:13 AM
#4
Re: [3.0/LINQ] Foreach loop problem?
Why aren't you using typeof to check the control type first?
if myControl.GetType() == typeof(MyCustomControl)
And I don't think there's a point to doing a foreach loop with MyCustomControl. Let it be Control instead, because this.controls will be a collection of Controls. Make it loop by Control, check the type, then get the .tag out.
-
Jun 28th, 2008, 11:13 AM
#5
Re: [3.0/LINQ] Foreach loop problem?
Originally Posted by RudiVisser
Microsoft's solution to problems like this is as follows:
Code:
foreach(Control ctl in this.Controls) {
try {
MyCustomControl mycc = (MyCustomControl)ctl);
// do your if statement here since you know it's of type MyCustomControl
} catch { /* Silently handle Conversion errors */ }
}
It certainly is not.
csharp Code:
MyUserControl uc;
foreach (Control ctl in this.Controls)
{
uc = ctl as MyUserControl;
if (uc != null && uc.Tag as string == myString)
{
// This control is a MyUserControl and its Tag contains the string of interest.
}
}
The 'as' key word performs a cast and returns a null reference if it fails, without throwing an exception. Note that I have used 'as' to get the string from the Tag too, which is more appropriate than calling ToString. If the Tag refers to a string already then casting is more appropriate than calling ToString, and if it doesn't contain a string then you're not interested so getting a null reference is appropriate again.
-
Jun 28th, 2008, 11:18 AM
#6
Thread Starter
Lively Member
Re: [3.0/LINQ] Foreach loop problem?
Oh..
Had no idea, thanks!
Though, I ended up using:
Code:
foreach (Control myControl in this.Controls)
{
if (myControl is MyControl)
{
if (((MyControl)myControl).Tag.ToString() == myString)
{
myMethod();
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|