471,355 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 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 2037
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by PlasmaDragon | last post: by
3 posts views Thread by Ed Crowley | last post: by
4 posts views Thread by Ian Richardson | last post: by
reply views Thread by dustbort | last post: by
2 posts views Thread by =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.