473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Extending multiple Controls

Hello,

I want to extend multiple Controls like TextBox, Label, ComboBox etc. with
the same new featureset (for Example all need a Method getSomething())
Because I cant alter their Base-Class I have to write one new class for each
Control like MyTextBox, MyLabel, MyComboBox implementing
IMyControlInterface.
The Code for each Control is exactly the same but C# offers no way to
"include" some code.

Do I really have to write all the code into every Class? Isnt their an
elegant way to achieve this?

Any help greatly appreciated!

Best regards,
Wolfgang Hanke

Jun 27 '08 #1
5 2111
Wolfgang Hanke wrote:
Hello,

I want to extend multiple Controls like TextBox, Label, ComboBox etc.
with the same new featureset (for Example all need a Method getSomething())
Because I cant alter their Base-Class I have to write one new class for
each Control like MyTextBox, MyLabel, MyComboBox implementing
IMyControlInterface.
The Code for each Control is exactly the same but C# offers no way to
"include" some code.

Do I really have to write all the code into every Class? Isnt their an
elegant way to achieve this?

Any help greatly appreciated!

Best regards,
Wolfgang Hanke
If you can put the code into a common method/class you can call, then
you can do that, otherwise no, you need to place the code into each class.

You could do it like this:

1. Create a common class implementing IMyControlInterface
2. Create the new control classes (MyTextBox, etc.)
3. In the new classes, add a member field of type IMyControlInterface
4. In the constructor of the new classes, construct an instance of
the common class and store into the member field
5. Implement IMyControlInterface on the new classes, each method
and/or property would just read/write/call the corresponding
member on the object in the member field

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #2
If the code is the same... would it suffice to just have a method that
accepts Control? Or is it something specific you are doing?

Well, C# 3 extension methods could do what you describe; you'd still
need separate extension methods for each type, but they could all call
into the same implementation:

public static class ControlExt {
public static void DoSomething(this Label label)
{DoSomethingImp(label);}
public static void DoSomething(this TextBox textBox)
{DoSomethingImp(textBox);}
// etc
static void DoSomethingImp(Control control) {
// your code
}
}

Now against a Lable (for example) you will be able to use:
label1.DoSomething();

Marc
Jun 27 '08 #3
Thank you, I did not know about those extension methods. I will have a
closer look at them even though it sounds as if you should not use them
extensively...

Wolfgang

Jun 27 '08 #4
You could do it like this:
>
1. Create a common class implementing IMyControlInterface
2. Create the new control classes (MyTextBox, etc.)
3. In the new classes, add a member field of type IMyControlInterface
4. In the constructor of the new classes, construct an instance of
the common class and store into the member field
5. Implement IMyControlInterface on the new classes, each method
and/or property would just read/write/call the corresponding
member on the object in the member field
Yes, those were my thoughts, too. But Delphi offers so called "class
helpers" to extend classes where you dont have access to. So i thought there
could be something similar in C#.
Those Extension Methods mentioned by Marc seem to be the closest equivalent.

Does anyone know a Design Pattern for "extending" a base class in this way?
Dont want to invent a new wheel...

Wolfgang

Jun 27 '08 #5
Note that extension methods are just compiler trickery to make it *seem*
like there is an instance method (DoSomething) on the nominated control.
In reality they are just static methods that accept the instance as the
first argument, and they need the C# 3 compiler. But it might help...

Marc
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PlasmaDragon | last post by:
I have a class, let's call it "x". I have another class, y, which extends x. Then I have a class z which extends y. There is a function in y (but not in x) which I have overridden in z. But...
3
by: Ed Crowley | last post by:
How can I extend the MonthCalendar control to allow multiple date ranges to be selected? For example, I'd like to pass 01/01/2003 - 04/01/2003, 08/01/2003 - 14/01/2003 and have both ranges...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
8
by: Anthony Williams | last post by:
Morning all, I'm having a wee problem with a project I'm working on at the moment. I'm leading my company into producing a website, based upon Web Standards, which will be created using XHTML...
4
by: Ian Richardson | last post by:
Hi, The function I've put together below is a rough idea to extend a SELECT list, starting from: <body> <form name="bambam"> <select id="fred"> <option value="1">1</option> <option...
0
by: dustbort | last post by:
ASP.NET 1.1 I want to enhance the existing web controls to add special data-binding features like provider-consumer relationships and the ability to automatically add validation based on database...
0
by: vishnu | last post by:
Hello All, I have embedded python 2.5 in to my C application. As we need the python scripts to run in multi threaded environment I have used Py_NewInterpreter() and Py_EndInterpreter each time I...
2
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I am a newbie in AJAX technologies. For my application I need a combination of ListSearchExtender and FilteredTextBox control. I have downloaded the toolkit with source code, and seem to...
0
by: sunny74 | last post by:
Dear All, I want to create a new custom web control by extending the 1)HTMLInputFile control and 2)File Upload control The purpose of extending the control is to add the following functionalities...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.