Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to implement generic routines in any language (C#, Java, etc). By generic routines I mean, in specific event Handlers. Lets say, I have 2 Buttons and 2 text boxes. One Button, when pressed, takes the first text box text and converts to Uppercase and second button prints the length of the string in the other text box. Now, Can I have a function as

button(type, textbox) {
/*Convert to upper case*/
else if(type==2)
/*Print the length*/

The type takes any button as input and further processes it. I want to implement this because of redundancy in my code and I want to make my code portable to other languages. So that I need to modify only the syntax to make it work. Is it feasible?

share|improve this question

closed as too broad by gnat, jwenting, MichaelT, GlenH7, Bart van Ingen Schenau May 3 '14 at 14:15

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

This sort of thing is the least of your worries if you are going to port your code to different languages. Even porting to a different GUI framework in the same language is generally a huge job. Besides, having separate functions to do different things is a good idea, I don't know why you want to try to combine them into one function like this. – Greg Hewgill May 2 '14 at 2:21
Yes, it is possible in any Turing-complete language. But some languages and frameworks make this easier than others. C# and Winforms has Event Handlers that pretty much give you this functionality out of the box. – Robert Harvey May 2 '14 at 2:32
Can you give an example how to implement this in C# @RobertHarvey – subham soni May 2 '14 at 4:43
up vote 1 down vote accepted

Normally, this should be done by having one handler for each logic.

void handleUppercase(object sender, EventArgs args)
    // convert text to uppercase

void handleLength(object sender, EventArgs args)
    // display length

// then assign those handlers properly
button1.OnClick += handleUppercase
button2.OnClick += handleLength

Also, you are not going to have much luck changing code for different languages, because different languages use different UI frameworks. And there are too big differences between those frameworks to allow any kind of sharing. If you want to have same code in different languages, you have to make sure it doesn't depend on any specific framework. Which UI frameworks are.

The case is quite different when you have same logic, but on different buttons with different parameters. In this case, you have 2 basic options.

First is to use similar approach as Robert suggests:

void handleUppercase(object sender, EventArgs args)
    if (sender == buttonA) // do logic for first arguments
    if (sender == buttonB) // do logic for second arguments
buttonA.OnClick += handleUppercase
buttonB.OnClick += handleUppercase

Second option is to parametrize the handler itself. In OOP, that could be done if the handler was a Command. Simply create different instances of command with different parameters for each button. In functional way, you could use partial application. But because C# doesn't have this concept, you have to help yourself with lambdas:

void handleUppercase(object sender, EventArgs evArgs, Argument arg)
    // do logic for arg

buttonA.OnClick += (s, e) => { handleUppercase(s, e, arg1); }
buttonB.OnClick += (s, e) => { handleUppercase(s, e, arg2); }
share|improve this answer
@subhamsoni See my edit. – Euphoric May 2 '14 at 9:00

In C#, it would look something like this:

// Loop through the controls on the form, and do something
// specific with each control, based on its type.
foreach (Control control in form.Controls)
    switch (control.GetType().Name)
        case "TextBox":
            var textBox = (TextBox)control;
            // Do something with the text box
        case "ComboBox":
            var comboBox = (ComboBox)control;
            // Do something with the combo box
        case "CheckBox":
            var checkBox = (CheckBox)control;
            // Do something with the checkbox

This code works because all Controls in Winforms inherit from a base Control class.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.