jonmiller
jonmiller

Reputation: 1

Replace all instances of base class with child class C#

This is a bit hard to explain in the title, but here's my situation. I have existing code and use the standard Textbox throughout. I have decided that I need to add functionality to the Textbox class so I created MyTextbox derived from Textbox and added what I needed. Now I want to permanently use MyTextbox instead of Textbox. I Should be able to force all instances of Textbox (present and future) to use MyTextbox.

I have done something similar before in C++ along the tune of

    typedef Textbox MyTextbox;

If this makes sense, is it possible to do in C#?

Upvotes: 0

Views: 177

Answers (3)

EtherDragon
EtherDragon

Reputation: 2698

Another way to make sure that your new functionality is available to everyone who uses TextBox instead of MyTextBox is to use extension methods. Then you wouldn't have to worry about the user using the wrong class.

MSDN Article on Extension Methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx

Upvotes: 1

Bernard
Bernard

Reputation: 7961

Why not do a mass find/replace across your solution? This can be done quite easily in Visual Studio.

You could do the following at the top of each file that uses a Textbox:

using Textbox = Custom.Namespace.MyTextbox;

I don't recommend this because anyone seeing what looks like the standard text box in your code may not realize it is in fact your own custom text box. This may also cause ambiguity when compiling the related code.

Upvotes: 2

Muhammad Ali
Muhammad Ali

Reputation: 72

Why would you need to refer TextBox class again if you already have created MyTextBox. Remember, MyTextBox has all the functionality from TextBox and its own functionality as well.

In case you want to change all existing textboxes from your to comply to MyTextBox, you have to go manual. Edit them all because even if you change the type from .Net Framweork Base Class Library, exceptions would be thrown wherever a TextBox is called

Upvotes: 0

Related Questions