473,804 Members | 3,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
IMyControlInter face.
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 2126
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
IMyControlInter face.
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 IMyControlInter face
2. Create the new control classes (MyTextBox, etc.)
3. In the new classes, add a member field of type IMyControlInter face
4. In the constructor of the new classes, construct an instance of
the common class and store into the member field
5. Implement IMyControlInter face 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***@vk arlsen.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(thi s Label label)
{DoSomethingImp (label);}
public static void DoSomething(thi s 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.DoSometh ing();

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 IMyControlInter face
2. Create the new control classes (MyTextBox, etc.)
3. In the new classes, add a member field of type IMyControlInter face
4. In the constructor of the new classes, construct an instance of
the common class and store into the member field
5. Implement IMyControlInter face 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
3691
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 when I try to run the program, it uses the version of the function in y! I have heard that java doesn't support multiple inheritance, so could this be the problem? So basicall: is there a rule in java which I am violating by trying to have a...
3
8646
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 selected on the control. Any help would be appreciated!
3
2228
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 C# know which is extending and which is combining? interface IStorable{ void Read(); void Write(object o); }
8
1960
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 and CSS, and powered by ASP.NET. My first problem, which I'm near to solving, was that ASP.NET doesn't produce valid XHTML output. We don't want to spend money on third-party components, and we can't wait for ASP.NET 2.0, so we needed to find a
4
2282
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 value="2">2</option>
0
989
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 schema. The problem I am having is that most of this functionality is really an extension of the WebControl class, from which the web controls inherit. There is a lot of functionality (properties and methods) that I would like to put into a base...
0
1568
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 execute a run script function. The code is as follows: RunScript(char *pScriptName,char *pFuncName,...) { PyEval_AcquireLock()
2
1457
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 have an understanding on where to modify the controls in order to achieve what I need. I do not want to modify the toolkit source directly as those changes will be overwritten when the next update/version of AJAX will come out.
0
899
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 not provided by these controls: a.Selection of Folders b.Retrieval of the folder path Can someone tell me the proper way of doing it,so that I can drag it from the toolbox and us it.
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.