rfazendeiro Posted September 26, 2008 Posted September 26, 2008 I'm tring to implement a recursive method to check the type of a control. Since i extend controls (like i have a NumericTextbox) i want to check if its base type if of type Textbox. I could do control.GetType().BaseType.Basetype (this would return TextBox) but if someday i change things this would mean i would have to mess with this code to. My problem is with the recursive method. It always returns false no mater what, even if it executes the code (means the type is a textbox). if (controlType == compareType) return true; I'm guessing the problem is with my stop algorithm but cant figure out what to change. Can any one lend a hand here please? public FormObject SaveDataContext(Control container) { foreach (Control control in container.Controls) { if (!InheritsFrom(control.GetType(), typeof(TextBox)) continue; ... code here } } public Boolean InheritsFrom(Type controlType, Type compareType) { //Stop Condition. If the Type is control that means i dont need //to continue with recursion and can return false if (controlType == typeof(Control)) return false; //this means it's the same type of compareType so i can stop //recursion if (controlType == compareType) return true; //Pass the base type of the Type passed to see if base type is // the same as compareType InheritsFrom(controlType.BaseType, compareType); return false; } Quote
rfazendeiro Posted September 26, 2008 Author Posted September 26, 2008 I ask you ppl to ignore this question. I think i was having a brain fart or something <.< public Boolean InheritsFrom(Type controlType, Type compareType) { if (controlType == typeof(Control)) return false; return controlType == compareType || InheritsFrom(controlType.BaseType, compareType); } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.