Recursion problem

rfazendeiro

Centurion
Joined
Mar 8, 2004
Messages
110
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).
Code:
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?


Code:
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;
}
 
I ask you ppl to ignore this question. I think i was having a brain fart or something <.<


Code:
public Boolean InheritsFrom(Type controlType, Type compareType)
{
       if (controlType == typeof(Control))
           return false;
       return controlType == compareType || InheritsFrom(controlType.BaseType, compareType);
}
 
Back
Top