?
Solved

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

Posted on 2008-11-19
6
Medium Priority
?
321 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:Merion
6 Comments
 
LVL 9

Assisted Solution

by:amx
amx earned 450 total points
ID: 22996209
Have you considered using extension methods?
0
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 450 total points
ID: 22996239
dotnet framework 3,0 allows you to add static methods to existing classes but this is not available in framework 2.0

for more info and samples
http://msdn.microsoft.com/en-us/library/bb383977.aspx
http://weblogs.asp.net/dwahlin/archive/2008/01/25/c-3-0-features-extension-methods.aspx
0
 

Author Comment

by:Merion
ID: 22996325
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
ASliborsky earned 600 total points
ID: 22996427
In .NET 2.0 it's impossible.
It became possible only since .NET 3.0
The only way to extend functinoality in .NET 2.0 and lower is to inherit from the class.

There are another ways:
Way 1. Create Composite control:
1. You create an Interface with method you need (FindControlRecursive)
2. You create Control classes you need, inherit them from necessary control classes (e.g. from UserControl, Label etc)
3. Implement created previously interface with necessary method - you may even just call static implementatino or do it another appropriate way.

Way 2. Use AOP:
Use Aspect Oriented Programming: use proxies. Just using any AOP engine wrap controls you need and use this method.

Way 3. Consider another way of implementation.
Why do you need to have this method in your controls. May be it's possible to move it to another place? What is your task?
0
 
LVL 9

Expert Comment

by:amx
ID: 22996648
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

0
 

Author Comment

by:Merion
ID: 22996746
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question