Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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;
}

Posted

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);
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...