Link to home
Start Free TrialLog in
Avatar of Merion
MerionFlag for United States of America

asked on

How can I add a method to an existing pre-defined class in VS2005 .Net?

I'm working on a large-scale asp.net (vb) project, and  I'm wondering if there's a way to add a new method to the Control class.

Inheritance isn't really an option -- if I made ControlEx Inherits Control and added it there, I'd then need to create a WebControlEx, LabelEx, DropDownList, etc to inherit from that.

I tried creating a Partial Public Class Control and adding the method there (well, okay, I added a simple property for testing purposes), but it didn't work -- I'm pretty sure that's just creating a new "Control" class and not even trying to append to the pre-defined one.

I know this is possible in scripting languages like Javascript and Lua, but has anyone had any success with this in .Net?
Partial Public Class Control
 
    Private _test As Integer
 
    Public Property Test() As Integer
        Get
            Return _test
        End Get
        Set(ByVal value As Integer)
            _test = value
        End Set
    End Property
 
End Class

Open in new window

SOLUTION
Avatar of amx
amx
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Toms Edison
Toms Edison
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Merion

ASKER

We're running 2.0 SP1 so I don't think either of those things are available.

And it can't be static regardless.  Specifically, I'm looking to add something akin to a FindControlRecursive method that will recurse through the control hierarchy to find the control with the specified ID. The method is made and implemented for one control type, but I'd like to apply it to the Control class so that all controls can use it.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As an alternative to FindControlRecursive you could use this which uses generics:

You can read more about it here


public static T FindControl<T>(System.Web.UI.ControlCollection Controls) where T : class
{
     T found = default(T);
 
     if (Controls != null && Controls.Count > 0)
     {
          for (int i = 0; i < Controls.Count; i++)
          {
               if (Controls[i] is T)
               {
                    found = Controls[i] as T;
                    break;
               }
               else
                    found = FindControl<T>(Controls[i].Controls);
          }
     }
 
     return found;
}

Open in new window

Avatar of Merion

ASKER

While that's a good example of finding a control that matches a specific type, I'm not sure what it has to do with my question. :-)
The function I wrote matches by ID -- just like Control.FindControl does.



Anyhow, thanks to everyone for your help.  It was kind of a "nice-to-have" feature so I'm not going to worry about it any further.