How to clear textbox values with functional programming

Posted by Goldytech under C# category on | Views : 2742
There are lot of methods by which you can clear the values of your forms controls. But all those are traditional old ways of doing common things.
I introuduce to you a new concept of Functional Programming by which you can do the same tasks with less code and the same code can be reused for lot more common tasks.
Suppose I have static class called FunctionalExtensions

public static class FunctionalExtensions
public static void ForEach<T>(IEnumerable<T> items, Action<T> DoSomething)
foreach (T item in items)

This code has the static ForEach method which uses generic params.

IEnumerable<TextBox> Textboxes = (from Control c in this.PnlContainer.Controls
where c.GetType() == typeof(TextBox)
select c).AsEnumerable().Cast<TextBox>();
FunctionalExtensions.ForEach(Textboxes, ClearTextBox);

The above code queries the Panel Controls or it can be any container control through Linq and get all the textboxes present in the panel control.
Then using the ExtensionMethod ForEach we call another ClearTextBox method to do the stuff.

private void ClearTextBox(TextBox txtbox)
txtbox.Text = string.Empty;

So as you saw this code can be reused for any IEnumerable class. It also reduces the lines of code.

Hope this helps

Comments or Responses

Login to post response